【问题标题】:Spring-Integration: int-http:outbound-channel-adapter using jsonSpring-Integration:int-http:outbound-channel-adapter 使用 json
【发布时间】:2016-10-28 18:58:47
【问题描述】:

我想使用int-http:outbound-channel-adapter 调用我的rest 服务,但不幸的是它不想将我的pojo 转换为json。

我的配置:

<int-http:outbound-channel-adapter 
    channel="ticketOutgoingChannel"
    http-method="PUT"
    url="http://localhost:8080/process/ticket"
    expected-response-type="*.model.Ticket"
    message-converters="converters"
/>

<util:list id="converters">
    <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
    <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</util:list>

堆栈跟踪:

Caused by: org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [*.model.Ticket] and content type [application/x-java-serialized-object]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:810)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:594)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:572)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:493)
at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:382)
... 34 more

【问题讨论】:

    标签: json http type-conversion spring-integration


    【解决方案1】:

    [application/x-java-serialized-object]

    如果没有提供 contentType 标头,这是 Java 对象的默认内容类型。

    使用

    <int:header-enricher input-channel="ticketOutgoingPreProcessChannel" 
           output-channel="ticketOutgoingChannel">
        <int:header name="contentType" value="application/json" overwrite="true/>
    </int:header-enricher>
    

    指示适配器转换为 JSON。

    【讨论】:

    • 感谢您的回复。我选择了&lt;int-http:outbound-channel-adapter&gt;,因为我不期待回复。你能否解释一下我应该如何使用&lt;int:header-filter&gt;,也许一个小例子会很棒。我使用 &lt;int-http:inbound-channel-adapter&gt; 将 pojo 发送到集成上下文中。所以我猜它到达那里的标头设置为 x-java-serialized-object。
    • 我的错误——如果没有提供,那是默认的contentType。请参阅我的编辑。 &gt; I dont expect a response - 在这种情况下,你不需要 expected-response-type - 这就是我的想法。
    • 太棒了,这行得通。谢谢。顺便说一句。消息转换器也不再需要了。
    • 是的,我们默认配置一个 JSON 转换器(以及其他一些)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 2016-11-26
    相关资源
    最近更新 更多