我試圖使用Spring Integration(3.0.1)實現一個RESTful服務,該服務支持兩個 XML和JSON作爲請求,響應格式,使用int-http:入站網關。Spring Integration 3.0.1 Restful http-inbound-gateway不會將請求正文轉換爲對象
我的代碼是基於Spring的集成例子(儘管這並不使用消息的有效載荷):
https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/rest-http
服務Activator類:
@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>
當前,代碼日誌o UT:
Received: <CustomerServiceRequest><CustomerName>Robert Pulson</CustomerName></CustomerServiceRequest>
(或等效的JSON格式),並且返回在JSON或XML的響應,這取決於Accept報頭,以使得部分工作。
我已閱讀,是基於以前版本的Spring集成以下類似的問題:
Spring Integration : http:inbound-channel adapter - not getting json object in payload
但這採用了inbound-channel-adaptor
,而不是一個inbound-gateway
,因爲我有。
如何配置我的inbound-gateway
使用marshaller
和jaxbJacksonObjectMapper
(來自同一個配置文件)到原始請求主體轉換爲CreateOrderRequest
實例都JSON/XML嗎?
感謝您的回覆,但以上都沒有任何區別。入站網關沒有預期類型屬性。有沒有計劃更新GIT中的rest-http示例,並將正文轉換爲POST示例? –
抱歉 - 錯字,屬性是'request-payload-type'。 –
我只是通過添加'request-payload-type'來推送一個提交給我的分支,顯示'JSON-> Map'。 https://github.com/garyrussell/spring-integration-samples/commit/a5b46a4a41d4ab820015adbea9ceac3d70b663ea –