【问题标题】:Camel REST DSL API - Multiple Content Type - XML ErrorCamel REST DSL API - 多种内容类型 - XML 错误
【发布时间】: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


【解决方案1】:

我为骆驼打开了一个错误并且响应有效:

需要为您的 JAXB 类使用 jaxb.in​​dex 文件。或者您可以通过

关闭数据格式以要求 JAXBElement
.dataFormatProperty("mustBeJAXBElement", "false");

此配置在 RestConfiguration DSL 上进行,如下所示:

    @Override
    public void configure() throws Exception {
        restConfiguration()
            .component("servlet")
            .contextPath("/credits/v1")
            .enableCORS(true)
            .apiContextPath("/api-doc")
            .apiProperty("api.title", "REST API")
            .apiProperty("api.version", "v1")
            .apiContextRouteId("doc-api")
            .bindingMode(RestBindingMode.auto)
            .dataFormatProperty("prettyPrint", "true")
            .dataFormatProperty("mustBeJAXBElement", "false");

    }

欲了解更多信息,请查看:https://issues.apache.org/jira/browse/CAMEL-12217

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 2021-04-04
    • 2014-03-08
    相关资源
    最近更新 更多