【问题标题】:Spring Integration 3.0.1 Restful http-inbound-gateway doesn't convert request body into objectSpring Integration 3.0.1 Restful http-inbound-gateway 不会将请求正文转换为对象
【发布时间】:2014-04-26 04:37:02
【问题描述】:

我正在尝试使用 Spring Integration (3.0.1) 来实现一个 RESTful 服务,该服务支持 XML 和 JSON 作为请求和响应格式,使用 int-http:inbound-gateway。

我的代码基于 Spring 集成示例(尽管这不使用消息负载):

https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/rest-http

服务激活器类:

@Service("httpOrderGateway")
public class HttpOrderGateway implements OrderGateway {

    private static final Logger LOGGER = Logger.getLogger(HttpOrderGateway.class);

    @Override
    public Message<CreateOrderResponse> createOrder(Message<CreateOrderRequest> orderRequest) {
        LOGGER.info("Received CreateOrderRequest headers: " + orderRequest.getHeaders());
        LOGGER.info("Received: " + orderRequest.getPayload());

        return MessageBuilder.withPayload(new CreateOrderResponse("Thank you for your order")).build();
    }

}

Spring 集成 XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/integration 
                            http://www.springframework.org/schema/integration/spring-integration.xsd
                            http://www.springframework.org/schema/integration/http 
                            http://www.springframework.org/schema/integration/http/spring-integration-http.xsd
                            http://www.springframework.org/schema/oxm 
                            http://www.springframework.org/schema/oxm/spring-oxm.xsd"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:oxm="http://www.springframework.org/schema/oxm"
       xmlns:int-http="http://www.springframework.org/schema/integration/http">

    <int:annotation-config />  

    <int:channel id="orderRequestChannel" />
    <int:channel id="orderResponseChannel" />

    <int-http:inbound-gateway id="inboundOrderRequestGateway" 
                              supported-methods="POST"
                              request-channel="orderRequestChannel"
                              reply-channel="orderResponseChannel"
                              view-name="/order"
                              path="/services/order"
                              reply-timeout="50000">
    </int-http:inbound-gateway>

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1" />
        <property name="contentNegotiationManager">
            <bean class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
                <property name="defaultContentType" value="application/json" />
                <property name="mediaTypes">
                    <map>
                        <entry key="json" value="application/json" />
                        <entry key="xml" value="application/xml" />
                    </map>
                </property>
            </bean>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="com.anon.order.gateway.json.view.ExtendedMappingJacksonJsonView">
                    <property name="objectMapper" ref="jaxbJacksonObjectMapper" />
                </bean>
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <constructor-arg ref="marshaller" />
                </bean>
            </list>
        </property>
    </bean>

    <oxm:jaxb2-marshaller id="marshaller" contextPath="com.anon.order.gateway.marshalling.model.impl" />

    <int:service-activator id="orderGatewayActivator"
                               input-channel="orderRequestChannel"
                               output-channel="orderResponseChannel"
                               ref="httpOrderGateway" 
                               method="createOrder" 
                               requires-reply="true"
                               send-timeout="60000" />

    <bean id="jaxbJacksonObjectMapper" class="com.anon.order.gateway.json.JaxbJacksonObjectMapper" />

</beans>

目前,代码退出:

Received: <CustomerServiceRequest><CustomerName>Robert Pulson</CustomerName></CustomerServiceRequest>

(或等效的 JSON 格式),并以 JSON 或 XML 格式返回响应,具体取决于 Accept Header,因此该部分正常工作。

我已阅读以下基于 Spring Integration 先前版本的类似问题:

Spring Integration : http:inbound-channel adapter - not getting json object in payload

但这使用的是inbound-channel-adaptor,而不是我使用的inbound-gateway

如何配置我的inbound-gateway 以使用marshallerjaxbJacksonObjectMapper(来自同一个配置文件)将原始请求正文转换为JSON/XML 的CreateOrderRequest 实例?

【问题讨论】:

    标签: java spring spring-integration


    【解决方案1】:

    配置适当的HttpMessageConverters 并使用message-converters 属性注入它们。

    另请参阅merge-with-default-converters 属性。

    请参阅MappingJackson2HttpMessageConverter(杰克逊 2)、MappingJacksonHttpMessageConverter(杰克逊 1.x)和 MarshallingHttpMessageConverter

    此外,如果 JAXB 和 Json 消息转换器位于类路径中,则默认情况下会自动使用它们。如果转换不需要任何特殊操作,您可以在网关上设置request-payload-type

    请参阅reference documentation 了解更多信息。

    【讨论】:

    • 感谢您的回复,但以上都没有任何区别。入站网关没有预期类型属性。是否有任何计划用 POST 示例更新 GIT 中的 rest-http 示例并转换正文?
    • 抱歉,打错了,属性是request-payload-type
    • 我刚刚通过添加request-payload-type 向我的分支推送了一个显示JSON-&gt;Map 的提交。 github.com/garyrussell/spring-integration-samples/commit/…
    • 解决了 - 感谢您的帮助,非常感谢。由于某种原因,请求有效负载类型没有在 IDE 中自动完成,所以我不知道我可以使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    相关资源
    最近更新 更多