简体   繁体   English

RestTemplate.postForObject()有效,但是exchange()无效

[英]RestTemplate.postForObject() works, but exchange() does not

I have an application that sends api calls to another application, with this code: 我有一个使用以下代码将api调用发送到另一个应用程序的应用程序:

objreq.setReq(request);
HttpEntity<Request> entity = new HttpEntity<Request>(objreq, headers);

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

where headers contains application/json of course. 标题当然包含application / json。

The problem is, is that the objreq is set correctly, however, when it calls the other api: 问题是,但是在调用另一个api时,objreq设置正确:

@PostMapping
  public ResponseEntity<Response> insertRequest(
          @RequestBody @Valid Request request) {
    requestService.insertRequest(request);
    return ResponseEntity.ok(new ResponseSuccess<>());
  }

all the fields are null. 所有字段均为空。 This is really annoying as the fields are set correctly in the request, but in everything is null in the response. 这确实很烦人,因为在请求中正确设置了字段,但在响应中所有内容均为null。

Note: this code works fine 注意:此代码可以正常工作

restTemplate.postForObject(url, request, String.class);

Would like to know why the exchange does not work, unfortunately I don't have much experience with RestTemplate, so not sure if the issue is here. 想知道为什么交换不起作用,不幸的是我对RestTemplate没有太多经验,所以不确定问题是否在这里。 Many Thanks 非常感谢

根据本文档ok(T body)从参数T body构造ResponseSuccess T body ,您传递了ResponseSuccess对象,这可能是这里的问题,我不知道ResponseSuccess类的内部内容(无法查找此类的文档,因此我无法确定问题可能在此类中的什么地方),但我会在那里查找问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RestTemplate.postForObject 和列表 - RestTemplate.postForObject and Lists Spring 启动 - restTemplate.postForObject - 参数是 null - Spring boot - restTemplate.postForObject - params are null restTemplate.postForObject 没有发送 object 的所有成员变量 - restTemplate.postForObject not sending all the member variables of object 在使用微服务时通过 restTemplate.postForObject() 接收空列表 - Recieving Empty list via restTemplate.postForObject() while consuming a microservice 使用 RestTemplate.postForObject 调用 API 时出现 400 Bad Request - 400 Bad Request when using RestTemplate.postForObject to call API Spring Boot RestTemplate.postForObject 到 Firebase 消息不返回 - Spring Boot RestTemplate.postForObject to Firebase Messaging not returning 返回ResponseEntity和Http错误时,RestTemplate.postForObject返回Null - RestTemplate.postForObject Returns Null When ResponseEntity and Http Error Are Returned 模拟的restTemplate.postForObject未在测试执行中使用 - Mocked restTemplate.postForObject is not being used in execution of test RestTemplate.postForObject - 错误:org.springframework.web.client.HttpClientErrorException:400错误请求 - RestTemplate.postForObject - Error: org.springframework.web.client.HttpClientErrorException: 400 Bad Request RestTemplate 交换超时但 PostMan 有效 - RestTemplate Exchange Timeouts but PostMan Works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM