【发布时间】:2018-01-30 21:27:42
【问题描述】:
我正在尝试通过 Java DSL 创建一个 Camel REST 服务,该服务可以使用/生成 json 和 xml,并且 JSON 可以工作,但是当我尝试以 XML 检索结果时收到错误消息。
RestMethods 类:
公共类 RestMethods 扩展 RouteBuilder {
@Override
public void configure() throws Exception {
rest()
.bindingMode(RestBindingMode.json_xml)
.consumes("application/json, application/xml")
.produces("application/json, application/xml")
.get("/rating")
.toD("direct:getRatingByClient");
}
路线实施:
public class GetRatingByClientRoute extends RouteBuilder{
// Use to created the mock
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public void configure() throws Exception {
from("direct:getRatingByClient")
// Request
.to("log:init")
// Mediation
.process(exchange -> {
// TODO Implement route
TestEntity test = new TestEntity();
test.setTestAttribute("Teste");
exchange.getOut().setBody(test);
// Response
.to("log:end");
}
}
当我使用 Content-Type 运行时,application/json 就像一个魅力。 但是,当我使用 Content-Type application/xml 运行时,我收到了这个错误: java.io.IOException:org.apache.camel.InvalidPayloadException:没有可用的类型:javax.xml.bind.JAXBElement 但具有值:类 GetRatingByClientRouteResponse
按照我正在测试的申请: 卷曲-X GET \ 'http://localhost:8080/credits/v1/rating?client_cpf_cnpj=11111111111' \ -H '接受:应用程序/xml' \ -H '缓存控制:无缓存' \ -H '内容类型:应用程序/xml'
【问题讨论】:
-
不确定我是否遵循,但您的 GetRatingByClientRoute 类仅返回一个 json。它在哪里返回 xml 消息?
-
我更新了示例,使其更加清晰。我正在尝试使用 Apache Camel REST DSL 构建具有多种内容类型的 REST API。但是当我使用 application/xml Contenty Type 我得到错误 InvalidPayloadException。
-
不要使用exchange.getOut() 使用exchange.getIn().setBody(...)
-
我现在用 getIn 尝试了 rigth,但问题仍然存在!
-
请在设置交换之前登录以确保您返回有效的 xml。
标签: java json xml apache-camel