如何使用XML类型发布并得到答案作为java.lang.String在骆驼中



我正在尝试制作一些小消息转换器,以将新类型的数据注入现有系统。为此,我将新的XML转换为内部类,执行操作,并将结果作为String返回。当我将答案部署为XML时,没有问题,但是目前出现以下错误:

java.io.IOException: org.apache.camel.InvalidPayloadException: No body available of type: javax.xml.bind.JAXBElement but has value: 00 of type: java.lang.String on: Message: 00. Exchange[ID-60345-1455623194156-43-5][Message: 00]

我试图替换生产类型,outType,通过将内容类型设置为文本/纯文本来调整标题,但它确实有帮助。最初的绑定模式是 XML。我的目标是使用 POST 方法返回简单的字符串。

<restConfiguration component="netty4-http" 
                  bindingMode="auto" 
                  contextPath="/WebServices/rest"
                  enableCORS="true">
   <endpointProperty key="nettySharedHttpServer" value="#sharedNettyHttpServer"/> 
   <dataFormatProperty key="prettyPrint" value="false"/>
</restConfiguration>
<!-- defines the rest services using the context-path /user -->
<rest path="/service" 
     consumes="application/xml" 
     produces="text/plain">
   <description>REST service</description>
   <post uri="/request" 
         type="org.company.generated.VmML" 
         outType="java.lang.String">
       <route>
            <to uri="bean:authenticationBean?method=checkAuthentication"/>
            <bean ref="messageTranslator" method="vmmlToEntry"/>
            <to uri="bean:Service?method=store"/>
            <bean ref="messageTranslator" method="returnEntryToStringReplay"/>
       </route>
   </post>           
</rest>

我无法让我的示例使用 bindingMode="auto",所以我无法验证这是否真的是问题所在,但这对我有用:

<camelContext id="rest-test" allowUseOriginalMessage="false" xmlns="http://camel.apache.org/schema/blueprint" streamCache="false">
    <dataFormats>
        <jaxb id="jaxb" prettyPrint="false" contextPath="my.domain.classes" />
    </dataFormats>
    <restConfiguration component="jetty" scheme="http" host="0.0.0.0" port="9001" contextPath="/string/conversion" bindingMode="off" />
    <rest path="/test" id="rest-string-conversion" produces="text/plain" consumes="application/xml">
        <post>
            <route>
                <camel:unmarshal ref="jaxb"/>
                <camel:transform>
                    <camel:simple>${body.eventId}</camel:simple>
                </camel:transform>
                <log message="Event ID: ${body}" loggingLevel="INFO" logName="string-conversion-test" />
            </route>
        </post>
    </rest>
</camelContext>

响应是一个字符串,其中只有我从路由中收到的Event对象中提取的事件 ID。但是,响应的Content-Type标头被错误地设置为应用程序/xml而不是文本/纯文本:

200 OK
Content-Type:  application/xml
[..]
Transfer-Encoding:  chunked
Server:  Jetty(9.2.10.v20150310)
d9870180-257a-11e5-b345-feff819cdc9f

可能您的响应实际上确实包含一个字符串,但您的客户对 Content-Type 标头感到困惑?

最新更新