spring RestTemplate抛出通用400 Bad Request,但自定义服务器发送的消息未丢失

svujldwt  于 2024-01-05  发布在  Spring
关注(0)|答案(3)|浏览(210)

我们有一个带有POSTMap的rest控制器API,它接受一个对象作为请求参数,并在DB上创建相同的对象。我们对输入对象进行了一些验证,如果有任何错误,那么我们将使用自定义消息抛出异常。
如果我们从postman调用这个API,我们会看到相应的错误。
然而,当我们在其他应用程序的caller方法中使用Spring的RestTemplate调用它时,我们所看到的只是一个400 Bad请求和空的body。没有错误消息。
这里可能有什么问题。我们如何从API中获得自定义消息。
下面是我们如何使用rest模板调用API。

  1. String url = "https://localhost:8895/servuceUrl";
  2. HttpHeaders headers = new HttpHeaders();
  3. headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
  4. HttpEntity<AddRequest> entity = new HttpEntity<AddRequest>(obj, headers);
  5. ResponseEntity<String> data = null;
  6. adddata = restTemplate.exchange(syncurl, HttpMethod.POST, entity, String.class);

字符串
在服务器端,我们有一个异常,

  1. @ResponseStatus(value=HttpStatus.BAD_REQUEST)
  2. public class InvalidDataException extends Exception {
  3. public InvalidDataException(String msg) {
  4. super(msg);
  5. }
  6. }


控制器看起来像

  1. @PostMapping(RestUriMappings.POST_MAPPING)
  2. public ResponseDto add(@Valid @RequestBody AddRequest data) throws InvalidDataException
  3. {
  4. logger.debug("Incoming data for add: {}", data.toString());
  5. // validate the payload
  6. if(data.getName()==null)
  7. throw new InvalidDataException("Name shouldnt be null");
  8. }

liwlm1x9

liwlm1x91#

Spring的RestTemplate实现有一种奇怪的功能。每当响应是4XX时,它都会抛出HttpClientErrorException而不是返回响应。同样,当响应是5XX时,它会抛出HttpServerErrorException
当您进入库中时,您将遇到负责DefaultResponseErrorHandler.java#handleError(ClientHttpResponse response)中此类功能的代码片段。
所以,如果你想获取原始的4XX或5XX响应,你必须在RestTemplate.java#exchange()方法上写一个 Package 器。类似于这样的东西-

  1. private ResponseEntity<String> exchange(String url, HttpMethod method, HttpEntity<?> httpEntity,
  2. Class<?> class1, Map<String, String> paramMap) {
  3. ResponseEntity<String> responseEntity = null;
  4. try {
  5. responseEntity = restTemplate.exchange(url, method, httpEntity, String.class, paramMap);
  6. }catch(HttpClientErrorException e) {
  7. responseEntity = new ResponseEntity<>(e.getResponseBodyAsString(), HttpStatus.BAD_REQUEST);
  8. }catch(HttpServerErrorException e) {
  9. responseEntity = new ResponseEntity<>(e.getResponseBodyAsString(), HttpStatus.INTERNAL_SERVER_ERROR);
  10. throw e;
  11. }catch(Exception e) {
  12. responseEntity = new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
  13. throw e;
  14. }
  15. return responseEntity;
  16. }

字符串

展开查看全部
oogrdqng

oogrdqng2#

您缺少:

  1. headers.setContentType(MediaType.APPLICATION_JSON);

字符串

rnmwe5a2

rnmwe5a23#

根据Mukul Bansal的回答,我做了以下修改。

  1. public class MyErrorHandler extends DefaultResponseErrorHandler {
  2. @Override
  3. protected void handleError(ClientHttpResponse response, HttpStatusCode statusCode) throws IOException {
  4. }
  5. }

字符串
没有任何内容的错误方法。
然后将错误处理程序设置为RestTemplate。

  1. RestTemplate restTemplate = new RestTemplate();
  2. restTemplate.setErrorHandler(new MyErrorHandler());


然后返回带有实际状态码的ResponseEntity。
无论如何,我不能理解默认Spring实现的原因。

展开查看全部

相关问题