【问题标题】:Outbound Gateway post request to spring MVC controller出站网关向 Spring MVC 控制器发布请求
【发布时间】:2014-04-18 05:30:52
【问题描述】:

我尝试通过出站网关将类型为 MultiValueMap 的有效负载发送到 Spring MVC 控制器。但似乎地图数据没有到达控制器。不知道有什么问题或遗漏。我的代码是这样的:

出站配置:

 <int-http:outbound-gateway id="outGateway"
    http-method="POST"
    request-channel="responseChannel"
    url="http://localhost:8081/SpringIntegration-In-out-test/responseReq"
    extract-request-payload="true">
 </int-http:outbound-gateway>

控制器配置:

@RequestMapping("/responseReq")
public ModelAndView goResponse(@RequestBody MultiValueMap<String,String> body){

    Iterator  it = body.entrySet().iterator();
    while (it.hasNext()) {
        MultiValueMap.Entry pairs = (MultiValueMap.Entry)it.next();
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
        it.remove(); 
    }
    return new ModelAndView("response");
}

我使用 Iterator 来获取地图值,但它什么都没有。

【问题讨论】:

    标签: java spring spring-mvc spring-integration


    【解决方案1】:

    一切看起来都不错。

    1. 我建议您“嗅探”您的 HTTP 流量并查看您的身体情况。在 Windows 上,我使用 TCP Trace。在这里您应该确定您确实发送了一个Map 作为有效负载。

    2. 从另一端单独测试您的Controller,例如使用一些 RestClient。我的偏好 - Firefox plugin。此处手动构建表单,在DispatcherServlet中转换为MultiValueMap,不要忘记显示标题Content-Type: application/x-www-form-urlencoded

    【讨论】:

      【解决方案2】:

      如果要以单向方式(仅发送)使用出站适配器,则应使用outbound-channel-adapter

      <int-http:outbound-channel-adapter
          id="outAdapter"
          channel="responseChannel"
          url="http://localhost:8081/SpringIntegration-In-out-test/responseReq"
          http-method="POST"
          charset="UTF-8"
      />
      

      其次,通过将RequestMethod 定义为POST,使您的请求映射更加精确:

      @RequestMapping(value="/responseReq", method=RequestMethod.POST)
      

      最后,为什么要通过设置 extract-request-payload="true" 将 Spring 集成消息转换为 JMS 消息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-08
        • 2021-08-09
        • 2019-12-26
        • 2016-05-17
        • 1970-01-01
        • 2019-08-06
        相关资源
        最近更新 更多