使用JSON

时间:2018-03-19 01:03:11

标签: spring-integration

我尝试使用http:outbound-gateway从Spring Integration进行POST restful api调用。但我无法弄清楚如何将请求JSON对象传递给restful API。有人可以告诉我该怎么做吗?谢谢!

这是我的spring-integration-config.xml

<si:channel id="request.channel" />

<si:channel id="response.channel">
    <si:queue capacity="10" />
</si:channel>

<int-http:outbound-gateway id="employeeinfoGateway"
    request-channel="request.channel" 
    url="http://localhost:8080/EmployeeInfo"
    http-method="POST" 
    expected-response-type="java.lang.String" 
    charset="UTF-8"
    reply-timeout="5000" 
    reply-channel="response.channel">
</int-http:outbound-gateway>

这是我的Java代码:

MessageChannel request = (MessageChannel) context.getBean("request.channel");       

    JSONObject obj = new JSONObject();
    obj.put("empId", "100");

    request.send(MessageBuilder.withPayload(obj).setHeader("content-type","application/json").build());

以下是我收到的错误消息:

Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONObject] and content type [application/json], failedMessage=GenericMessage [payload={"empId":"100"}, headers={content-type=application/json, id=a0aba65f-3f49-4dfc-2431-51983666772b, timestamp=1521419315405}]

1 个答案:

答案 0 :(得分:1)

不确定JSONObject是什么。对我来说似乎不是杰克逊JSON处理器如何工作,但我建议尝试只发送一个简单的java.util.Map

下面的MappingJackson2HttpMessageConverter中有RestTemplate可将传入的有效负载转换为JSON表示。

也许你只是在课程路径中没有com.fasterxml.jackson.core:jackson-databind jar?