How to bind or convert response pojo to XML/JSON in Camel 2.14.1 REST DSL



I am trying to use REST DSL in Apache Camel.


I would like the route to bind/format the response pojo to XML or JSON format based on the request payload. If request payload is JSON, response should be in JSON format and if payload is XML, response should be in XML.


However, this is not happening. The response is always in XML only. When binding mode is RestBindingMode.json then only it gives JSON response.


Please help me find what I am doing wrong here! What else can be done to get the desired results?


Route Builder:



restConfiguration().component("servlet").bindingMode(RestBindingMode.json_xml)
.dataFormatProperty("prettyPrint", "true").contextPath("test/profile").port(8080);

rest("/getProfile").description("User profile services")

.post().description("Find profile by ID").type(Request.class).outType(Response.class)
.to("direct:userId-post");

from("direct:userId-post").routeId("profileRoute").processRef("profileProcessor");


Processor:



{
Response response = new Response();
Request request = (Request) exchange.getIn().getBody();
List<Profile> profileList = profileService.getProfile(request.getId()); // Calling method to populate the list
response.setStatusCode("200");
response.setStatusDesc("success");
response.setStatusType("SUCCESS");
response.setUserProfiles(profileList);
exchange.getOut().setBody(response);
}


POJO to bind rquest payload:



@XmlRootElement(name = "profile")
@XmlAccessorType(XmlAccessType.FIELD)
public class Request {
@XmlElement(name = "id")
private String id;
// Getters-Setters
}


Response POJO:



@XmlRootElement(name = "profiles")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
@XmlElement(name = "statusCode")
private String statusCode;
@XmlElement(name = "statusDescription")
private String statusDesc;
@XmlElement(name = "statusType")
private String statusType;
@XmlElement(name = "profile")
private List<Profile> userProfiles;
// Getters-Setters
}

No comments:

Post a Comment