I have a sample REST service with POST method running in my local system. This method is used to add user details.
The URL is 'http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/add'
Request is
{ "id" : "12346" , "name":"kswaughs" }
Response is
{
"id": "12346",
"status": "Success"
}
Create a POJO class to send the request
Request Object - UserRequest
package com.example.cxfrs.beans;
public class UserRequest {
private String id;
private String name;
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;
}
}
Create a POJO class to parse the JSON response into java object
Response Object - UserResponse
package com.example.cxfrs.beans;
public class UserResponse {
private String status;
private String id;
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;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("UserResponse [status=");
builder.append(status);
builder.append(", id=");
builder.append(id);
builder.append("]");
return builder.toString();
}
}
A simple stand-alone client code using WebClient and JacksonJsonProvider.
Sample Client code to call POST Method
package com.example.cxfrs;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.Response;
import com.example.cxfrs.beans.UserRequest;
import com.example.cxfrs.beans.UserResponse;
import org.apache.cxf.jaxrs.client.WebClient;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
public class CXFRestPostClient {
public static void main(String[] args) {
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
UserRequest req = new UserRequest();
req.setId("1234578");
req.setName("kswaughs");
WebClient client = WebClient
.create("http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/add", providers);
client = client.accept("application/json").type("application/json");
Response r = client.post(req);
UserResponse resp = r.readEntity(UserResponse.class);
System.out.println(resp);
}
}
Output is
UserResponse [status=Success, id=1234578]
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 JSON parsing error.
org.apache.cxf.jaxrs.utils.JAXRSUtils logMessageHandlerProblem
SEVERE: No message body writer has been found for class org.example.cxfrs.beans.UserRequest, ContentType: application/json
Nov 25, 2015 3:00:48 PM org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
WARNING: Interceptor for {http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/add}WebClient has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: No message body writer has been found for class org.example.cxfrs.beans.UserRequest, ContentType: application/json
at org.apache.cxf.jaxrs.client.WebClient$BodyWriter.doWriteBody(WebClient.java:1227)
at org.apache.cxf.jaxrs.client.AbstractClient$AbstractBodyWriter.handleMessage(AbstractClient.java:1052)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.jaxrs.client.AbstractClient.doRunInterceptorChain(AbstractClient.java:636)
at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1091)
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:331)
at org.apache.cxf.jaxrs.client.WebClient.post(WebClient.java:340)
at org.example.cxfrs.CXFRestPostClient.main(CXFRestPostClient.java:30)
Caused by: javax.ws.rs.ProcessingException: No message body writer has been found for class org.example.cxfrs.beans.UserRequest, ContentType: application/json
at org.apache.cxf.jaxrs.client.AbstractClient.reportMessageHandlerProblem(AbstractClient.java:754)
at org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:485)
at org.apache.cxf.jaxrs.client.WebClient$BodyWriter.doWriteBody(WebClient.java:1222)
... 9 more
Exception in thread "main" javax.ws.rs.ProcessingException: org.apache.cxf.interceptor.Fault: No message body writer has been found for class org.example.cxfrs.beans.UserRequest, ContentType: application/json
at org.apache.cxf.jaxrs.client.WebClient.doResponse(WebClient.java:1147)
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:331)
at org.apache.cxf.jaxrs.client.WebClient.post(WebClient.java:340)
at org.example.cxfrs.CXFRestPostClient.main(CXFRestPostClient.java:30)
Caused by: org.apache.cxf.interceptor.Fault: No message body writer has been found for class org.example.cxfrs.beans.UserRequest, ContentType: application/json
at org.apache.cxf.jaxrs.client.WebClient$BodyWriter.doWriteBody(WebClient.java:1227)
at org.apache.cxf.jaxrs.client.AbstractClient$AbstractBodyWriter.handleMessage(AbstractClient.java:1052)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.jaxrs.client.AbstractClient.doRunInterceptorChain(AbstractClient.java:636)
at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1091)
... 5 more
Caused by: javax.ws.rs.ProcessingException: No message body writer has been found for class org.example.cxfrs.beans.UserRequest, ContentType: application/json
at org.apache.cxf.jaxrs.client.AbstractClient.reportMessageHandlerProblem(AbstractClient.java:754)
at org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:485)
at org.apache.cxf.jaxrs.client.WebClient$BodyWriter.doWriteBody(WebClient.java:1222)
... 9 more
Related Links
Apache Camel SOAP Service Client Apache CXF Rest Client to call GET method
Really helpful
ReplyDelete