簡體   English   中英

Spring Integration 3.0.1 Restful http-inbound-gateway不會將請求主體轉換為對象

[英]Spring Integration 3.0.1 Restful http-inbound-gateway doesn't convert request body into object

我試圖使用Spring集成(3.0.1)來實現, 同時支持XML和JSON請求和響應格式,使用INT-HTTP RESTful服務:入站網關。

我的代碼基於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格式),並根據Accept Header返回JSON或XML中的響應,以便該部分正常工作。

我已經閱讀了以下基於以前版本的Spring Integration的類似問題:

Spring集成:http:入站通道適配器 - 不在有效負載中獲取json對象

但這會使用inbound-channel-adaptor ,而不是我所擁有的inbound-gateway

如何配置我的inbound-gateway使用marshallerjaxbJacksonObjectMapper (來自相同的配置文件)將原始請求主體轉換為JSON / XML的CreateOrderRequest實例?

配置適當的HttpMessageConverter並使用message-converters屬性注入它們。

另請參見merge-with-default-converters屬性。

請參閱MappingJackson2HttpMessageConverter (jackson 2), MappingJacksonHttpMessageConverter (jackson 1.x)和MarshallingHttpMessageConverter

此外,如果它們位於類路徑上,則默認情況下會自動使用JAXB和Json消息轉換器。 如果轉換不需要任何特殊內容,您可以在網關上設置request-payload-type

有關更多信息,請參閱參考文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM