【问题标题】:How to POST with XML type and get answer as java.lang.String in camel?如何使用 XML 类型发布并在骆驼中以 java.lang.String 形式获得答案?
【发布时间】:2016-02-16 12:36:41
【问题描述】:

我正在尝试制作一些小型消息翻译器,以将新类型的数据注入现有系统。为此,我将新的 XML 转换为内部类,执行操作,并将结果作为字符串返回。当我将答案部署为 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,通过将 Content-Type 设置为 text/plain 来调整标题,但它没有帮助。最初的 bindingMode 是 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="off"是否有效?那时你必须自己做 JAXB 解组,但如果这是你想做的,没有什么会干扰你返回一个纯字符串。
  • 我没有尝试启动模式。我尝试了 xml、xml_json、自动。最后我删除了这个属性,它开始工作了。我不知道为什么它会覆盖我对单个休息定义的设置。

标签: java xml apache-camel blueprint-osgi


【解决方案1】:

我无法让我的示例使用 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 标头被错误地设置为 application/xml 而不是 text/plain:

200 OK
Content-Type:  application/xml
[..]
Transfer-Encoding:  chunked
Server:  Jetty(9.2.10.v20150310)

d9870180-257a-11e5-b345-feff819cdc9f

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

【讨论】:

  • 我的 REST 客户端中的答案类型始终为纯文本/文本
猜你喜欢
  • 2021-12-11
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多