I have a sample REST service with GET method running in my local system. This method is used to fetch user details..
The URL is 'http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/details/123'
Response is
{
"status": "Success",
"id": "123",
"name": "kswaughs"
}
Create a model object to parse JSON response into java object.
Response Object - User
package com.example.cxfrs.beans;
public class User {
private String status;
private String id;
private String name;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("User [status=");
builder.append(status);
builder.append(", id=");
builder.append(id);
builder.append(", name=");
builder.append(name);
builder.append("]");
return builder.toString();
}
}
A simple stand-alone client code using WebClient and JacksonJsonProvider.
Sample Client code to call get Method
package com.example.cxfrs;
import java.util.ArrayList;
import java.util.List;
import com.example.cxfrs.beans.User;
import org.apache.cxf.jaxrs.client.WebClient;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
public class CXFRestClient {
public static void main(String[] args) {
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
WebClient client = WebClient
.create("http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/details", providers);
User user = client.accept("application/json").type("application/json")
.path("123").get(User.class);
System.out.println(user);
}
}
Output is
User [status=Success, id=123, name=kswaughs]
Use Below maven dependency in your pom.xml to use CXF Client
<!-- To use WebClient API --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>3.1.2</version> </dependency> <!-- To parse JSON String to Java objects --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.0</version> </dependency>
If you dont provide JacksonJsonProvider to WebClient, You will get below error while parsing the JSON response.
org.apache.cxf.jaxrs.utils.JAXRSUtils logMessageHandlerProblem SEVERE: No message body reader has been found for class com.example.cxfrs.beans.User, ContentType: application/json Exception in thread "main" javax.ws.rs.client.ResponseProcessingException: No message body reader has been found for class org.apache.camel.example.cxfrs.beans.User, ContentType: application/json at org.apache.cxf.jaxrs.impl.ResponseImpl.reportMessageHandlerProblem(ResponseImpl.java:433) at org.apache.cxf.jaxrs.impl.ResponseImpl.doReadEntity(ResponseImpl.java:384) at org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:512) at org.apache.cxf.jaxrs.client.WebClient.handleResponse(WebClient.java:1173) at org.apache.cxf.jaxrs.client.WebClient.doResponse(WebClient.java:1156) at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1092) at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:894) at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:865) at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:428) at org.apache.cxf.jaxrs.client.WebClient.get(WebClient.java:611) at com.example.cxfrs.CXFRestClient.main(CXFRestClient.java:21)
Thanks for the article. It helped me a lot.
ReplyDeleteThis helped me as well. Thanks for posting.
ReplyDeleteGreat read thaanks
ReplyDeleteF485821334
ReplyDeleteTakipçi Satın Al
3D Car Parking Para Kodu
MMORPG Oyunlar
MLBB Hediye Kodu
MFF Kupon Kodu