Eclipse 中响应正文为空

问题描述 投票:0回答:1

我正在使用restTemaplate 为Spring boot 应用程序进行post 调用。 200 没有响应主体,这很好。但对于其他 httpStatus,我可以看到在从邮递员而不是从 eclipse 进行 api 调用时有响应正文。

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("x-api-key", apiKey);
headers.set("User-Agent", "test");
ResponseEntity<String> responseEntity =  restTemplate.postForEntity(url,request, String.class);
Response Body from postman looks like this:
{"httpStatus":"INTERNAL_SERVER_ERROR","errorCode":"GENERAL_ERROR","message":"Exception thrown in the application"}

我也尝试使用交换,但同样的问题。 我期望获得除 200 之外的所有状态代码的响应正文。

java spring-boot resttemplate
1个回答
0
投票

你只打印正文吗

System.out.println("only body" + responseEntity.getBody());
或所有留言
System.out.println("responseEntity" + responseEntity );

你可以尝试一下吗

    ResponseEntity<String> responseEntity = restTemplate.postForObject(url, request, String.class);
System.out.println("responseEntity" + responseEntity );
System.out.println("only body" + responseEntity.getBody());

 ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
System.out.println("responseEntity" + responseEntity );
    System.out.println("only body" + responseEntity.getBody());
© www.soinside.com 2019 - 2024. All rights reserved.