I am trying to return POJO class reference to client in rest WS(CXF 3.1.2) as below,
Service method declaration :
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes({"application/xml", MediaType.TEXT_PLAIN})
@Path("/agentLogout")
public ResponseEvent agentLogout(String ext) {
ResponseEvent response= new ResponseEvent();
response.setDn(ext);
return response;
}
Client Code:
WebClient client = WebClient.create(REST_URI);
client.path("agentLogout").accept(MediaType.APPLICATION_JSON);
Response agentLogoutResponse = client.post("3101");
String responseStr=agentLogoutResponse.readEntity();
POJO:
public class ResponseEvent {
private String dn;
public String getDn() {
return dn;
}
public void setDn(String ext) {
this.dn=ext;
}
}
Question:
how i can retrieve/access the returned reference in client code ?
String responseStr=agentLogoutResponse.readEntity(); // Do i need to create the pojo class/interface in client code too..?
Whether this POJO reference rendered as JSON in CXF? if so how we can use it in client code?
Thanks,
Say You have created interface(It could be auto generated using WADL2Java plugin) at client side.
Create A singleton Class and Create instance of Service
**NOTE:**Here I'm using Jackson JAXB JSON proivder, which can marshal/unmarshal both xml and JSON you could use your choice of provider. An Jackson provider is not part of cxf so you need to include these dependency separately
And the You could access your pojo class as shown Below
1) You might want to have
MessageBodyWriter<ResponseEvent>
on server side. You might need to register this@Provider
. This would enable your POJO to be written to server's output stream. For instance (not tested):2) You need to have
MessageBodyReader<ResponseEvent>
on client side. For instance:You need to register this provider on client side. You do this for instance using JAXRS client, instead of cxf client (available since cxf 3), for instance with something like:
Then you can read your entity as