[英]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.