【问题标题】:Spring Integration http outbound gateway POST restful call with JSON使用 JSON 的 Spring 集成 http 出站网关 POST RESTful 调用
【发布时间】:2018-03-19 01:03:11
【问题描述】:

我尝试使用 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}]

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    不确定JSONObject 是什么。对我来说,这不是杰克逊 JSON 处理器的工作方式,但我建议尝试只发送一个普通的 java.util.Map 来代替。

    下面的RestTemplate 中有一个MappingJackson2HttpMessageConverter,用于将传入的有效负载转换为JSON 表示。

    也许您的类路径中没有com.fasterxml.jackson.core:jackson-databind jar?

    【讨论】:

    • 我尝试将 java.util.Map 传递到消息负载中,但没有成功。
    • 好的!你在类路径上有 Jackson jar 让RestTemplate 为 JSON 做艰苦的工作吗?
    • 是的,它有效。谢谢,阿尔特姆。我添加了 jackson-databind jar,然后将 java.util.Map 传递给消息有效负载。
    猜你喜欢
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 2012-03-07
    • 1970-01-01
    相关资源
    最近更新 更多