Java 如何在Camel 2.14.1 REST DSL中将响应pojo绑定或转换为XML/JSON
我正在尝试在ApacheCamel中使用RESTDSL 我希望路由能够根据请求负载将响应pojo绑定/格式化为XML或JSON格式。若请求负载为JSON,则响应应为JSON格式,若负载为XML,则响应应为XML格式 然而,这并没有发生。响应始终仅使用XML。当绑定模式为RestBindingMode.json时,只有它给出json响应 请帮我找出我做错了什么!还可以做些什么来获得期望的结果 路线生成器:Java 如何在Camel 2.14.1 REST DSL中将响应pojo绑定或转换为XML/JSON,java,xml,json,rest,apache-camel,Java,Xml,Json,Rest,Apache Camel,我正在尝试在ApacheCamel中使用RESTDSL 我希望路由能够根据请求负载将响应pojo绑定/格式化为XML或JSON格式。若请求负载为JSON,则响应应为JSON格式,若负载为XML,则响应应为XML格式 然而,这并没有发生。响应始终仅使用XML。当绑定模式为RestBindingMode.json时,只有它给出json响应 请帮我找出我做错了什么!还可以做些什么来获得期望的结果 路线生成器: restConfiguration().component("servlet").bindi
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:
@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
}
将内容类型头设置为application/json以获取json。我已经尝试过了。但它不起作用。无论我传递application/json还是application/xml,它都只给出xml响应。然而,要补充的是,当我将内容类型的值指定为application/json时,json负载是不被接受的。它抛出javax.xml.bind.UnmarshalException我正在为此使用RESTClient。请确保在ClassPath上有camel jackson我在pom中添加了camel jaxb和camel jackson依赖项,我可以在Libraries>Maven依赖项中看到这些JAR
@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
}