Spring集成如何在ws:outbound-gateway中处理Json Payload

时间:2017-08-21 11:03:49

标签: json spring spring-integration

我想使用Spring集成来调用webservice,然后将json响应放到FTP上,webservice需要json有效负载,我想使用webservice出站网关来满足我的请求,但是我得到了以下异常;似乎SimpleWebServiceOutboundGateway的默认处理程序不支持json格式有效负载,我该如何克服这个并正确传递JASON有效负载?

Warning:  org.apache.xerces.parsers.SAXParser: http://javax.xml.XMLConstants/property/accessExternalDTD
Warning:  org.apache.xerces.parsers.SAXParser: http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit
[Fatal Error] :1:1: Content is not allowed in prolog.
ERROR:  'Content is not allowed in prolog.'

org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.integration.handler.MessageHandlerChain#0$child#1.handler]

    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:84)
    at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:150)
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:114)
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:44)
    at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:93)
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendMessage(AbstractReplyProducingMessageHandler.java:260)
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendReplyMessage(AbstractReplyProducingMessageHandler.java:241)
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.produceReply(AbstractReplyProducingMessageHandler.java:205)
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleResult(AbstractReplyProducingMessageHandler.java:199)
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:177)
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
    at org.springframework.integration.handler.MessageHandlerChain.handleMessageInternal(MessageHandlerChain.java:131)
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
    at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:255)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:223)
    at com.oocl.frm.dmtp.company.esteelauder.OutboundGatewayStaticPostParameterTest.test(OutboundGatewayStaticPostParameterTest.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

My Spring集成配置:

<int:channel id="requestChannel"/>
    <int:channel id="xmlChannel">
        <int:queue/>
    </int:channel>

    <int:chain input-channel="requestChannel" output-channel="xmlChannel">
        <int:header-enricher>
            <int:header name="contentType" value="application/json"/>
        </int:header-enricher>
        <ws:outbound-gateway uri="http://localhost:8080/postService/post" >
        </ws:outbound-gateway>
        <int:transformer ref="jsonToXmlTransformer"/>
    </int:chain>

我的测试代码:

@RunWith(SpringJUnit4ClassRunner.class)
public class OutboundGatewayStaticPostParameterTest {
    @Autowired
    MessageChannel requestChannel;
    @Autowired
    QueueChannel xmlChannel;


    private static String JSON_STR= "{'userName':'iOSDeveloper','md5':'xxxxxxxxxxxxxxxxx' }";

    @Test
    public  void test() throws IOException, InterruptedException {
        Message<String> message = MessageBuilder.withPayload(JSON_STR).build();

        requestChannel.send(message);

        Message<String> outMsg = (Message<String>) xmlChannel.receive();

        System.out.println(outMsg.getPayload());

    }

}

和我宁静的网络服务代码

@Path("/postService")
public class PostService {
    @POST
    @Path("post")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public PostMessage postMethod(PostParameters postParameters) {
        PostMessage msg = new PostMessage();
        msg.setCode("200");
        msg.setMessage("Hello World!");
        return msg;
    }
}

2 个答案:

答案 0 :(得分:1)

WS Outbound Gateway适用于SOAP,但您尝试调用REST。考虑使用HTTP出站网关

答案 1 :(得分:0)

我正确的配置

 <int:channel id="requestChannel"/>
    <int:channel id="requestChannel2"/>
    <int:channel id="replyChannel"/>
    <int:channel id="xmlChannel">
        <int:queue/>
    </int:channel>


    <int:header-enricher input-channel="requestChannel"
                         output-channel="requestChannel2">
        <int:header name="Content-Type" value="application/json" overwrite="true"/>
</int:header-enricher>

    <http:outbound-gateway request-channel="requestChannel2"  expected-response-type="java.lang.String"
                           reply-channel="replyChannel"
                           url="http://localhost:8080/postService/postwithparm"
                           http-method="POST"
                           extract-request-payload="true">
    </http:outbound-gateway>

    <int:transformer ref="jsonToXmlTransformer" input-channel="replyChannel" output-channel="xmlChannel"/>

我的测试代码:

@Test
    public  void test() throws IOException, InterruptedException {
        PostParameters p = new PostParameters();
        p.setMd5("********");
        p.setUserName("io");
//        Message<String> message = MessageBuilder.withPayload(JSON_STR).build();
        Message message = MessageBuilder.withPayload(p).build();
        requestChannel.send(message);

        Message<?> outMsg = (Message<?>) xmlChannel.receive();

        System.out.println(outMsg.getPayload());

    }