【问题标题】:How to produce xml response using REST DSL in Apache Camel如何在 Apache Camel 中使用 REST DSL 生成 xml 响应
【发布时间】:2017-07-04 09:07:04
【问题描述】:

我试图在 Apache camel 中使用 rest DSL 来消耗和生成 xml 响应,但它以异常结束,我如何将我的输出 POJO 对象编组为 xml。下面是sy Apache Camel 路线

<camelContext id="camelContext-02bcd908-03ed-4c2c-878a-b87e3a3668ca"
        xmlns="http://camel.apache.org/schema/blueprint">
        <restConfiguration bindingMode="xml" component="servlet"
            contextPath="/camel-example-servlet-rest-blueprint/rest" port="8181">
            <dataFormatProperty key="mustBeJAXBElement" value="true" />
        </restConfiguration>
        <!-- defines the rest services using the context-path /user -->
        <rest consumes="application/xml" path="/user" produces="application/xml">
            <description>User rest service</description>
            <!-- this is a rest GET to view an user by the given id -->
            <get outType="org.apache.camel.example.rest.User" uri="/{id}">
                <description>Find user by id</description>
                <to uri="bean:userService?method=getUser(${header.id})" />
            </get>
            <!-- this is a rest GET to find all users -->
            <get outType="org.apache.camel.example.rest.User[]" uri="/findAll">
                <description>Find all users</description>
                <to uri="bean:userService?method=listUsers" />
            </get>
        </rest>
    </camelContext>

这是我遇到的异常

java.io.IOException: org.apache.camel.InvalidPayloadException: 没有可用的类型:javax.xml.bind.JAXBElement 但有值:[org.apache.camel.example.rest.User@7f98bc82, org .apache.camel.example.rest.User@4502b96] 类型:java.util.TreeMap.Values on:Message[ID-NISB-TEC-C3880-56991-1499145269945-37-3]。交换[ID-NISB-TEC-C3880-56991-1499145269945-37-2] 在 org.apache.camel.converter.jaxb.JaxbDataFormat.marshal(JaxbDataFormat.java:153) 在 org.apache.camel.processor.MarshalProcessor.process(MarshalProcessor.java:69) 在 org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109) 在 org.apache.camel.processor.MarshalProcessor.process(MarshalProcessor.java:50)

【问题讨论】:

    标签: java jaxb apache-camel


    【解决方案1】:

    您可以尝试将您的 bindingMode 更改为“关闭”。

    而当你创建get方法时,你可以使用“consumes”和“produces”参数等于“application/xml”,类似这样:

     <get outType="org.apache.camel.example.rest.User[]" uri="/findAll"consumes="application/xml" produces="application/xml">
        <description>Find all users</description>
        <to uri="bean:userService?method=listUsers" />
     </get>
    

    【讨论】:

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