如何在Camel 2.14.1 REST DSL中将响应pojo绑定或转换为XML / JSON

时间:2015-02-09 11:25:05

标签: java xml json rest apache-camel

我正在尝试在Apache Camel中使用REST DSL。

我希望路由根据请求有效负载将响应pojo绑定/格式化为XML或JSON格式。如果请求有效负载是JSON,则响应应为JSON格式,如果有效负载为XML,则响应应为XML。

但是,这种情况并没有发生。响应始终仅以XML格式显示。当绑定模式是RestBindingMode.json时,它只给出JSON响应。

请帮助我找到我在这里做错了什么!还有什么办法可以达到预期的效果?

路线建设者:

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");

处理器:

{
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绑定rquest有效负载:

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

回应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
}

0 个答案:

没有答案