Spring Integration 3.0.1 Restful http-inbound-gateway不会将请求主体转换为对象

Sas*_*imm 5 java spring spring-integration

我试图使用Spring集成(3.0.1),以实现支持RESTful服务 XML和JSON请求和响应格式,使用INT-HTTP:入站网关.

我的代码基于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();
    }

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

目前,代码注销:

Received: <CustomerServiceRequest><CustomerName>Robert Pulson</CustomerName></CustomerServiceRequest>
Run Code Online (Sandbox Code Playgroud)

(或等效的JSON格式),并根据Accept Header返回JSON或XML中的响应,以便该部分正常工作.

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

Spring集成:http:入站通道适配器 - 不在有效负载中获取json对象

但是这使用了一个inbound-channel-adaptor,而不是inbound-gateway像我一样.

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

Gar*_*ell 2

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

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

请参阅MappingJackson2HttpMessageConverter(jackson 2)、MappingJacksonHttpMessageConverter(jackson 1.x) 和MarshallingHttpMessageConverter

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

请参阅参考文档以获取更多信息。