Jackie Dong
Jackie Dong

Reputation: 813

Spring integration how to handle Json Payload in ws:outbound-gateway

I want to use Spring integration to call webservice and then get then put json response to FTP, webservice need json payload, I want to use webservice outbound gateway to fulfill my request, however I got following exception; seems SimpleWebServiceOutboundGateway's default handler not support json format payload, how can I overcome this and pass JASON payload correctly?

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 integration configuration:

<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>

My test code:

@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());

    }

}

and my restful webservice code

@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;
    }
}

Upvotes: 0

Views: 1457

Answers (2)

Jackie Dong
Jackie Dong

Reputation: 813

My right config

 <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"/>

My Test code:

@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());

    }

Upvotes: 0

Artem Bilan
Artem Bilan

Reputation: 121272

WS Outbound Gateway is for SOAP, but you try to call REST. Consider to use HTTP Outbound Gateway

Upvotes: 1

Related Questions