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

svujldwt  于 5个月前  发布在  Spring
关注(0)|答案(3)|浏览(95)

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

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

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

@ResponseStatus(value=HttpStatus.BAD_REQUEST)
public class InvalidDataException extends Exception {

    public InvalidDataException(String msg) {
        super(msg);
    }
}


控制器看起来像

@PostMapping(RestUriMappings.POST_MAPPING)
public ResponseDto add(@Valid @RequestBody AddRequest data) throws InvalidDataException 
{

    logger.debug("Incoming data for add: {}", data.toString());

    // validate the payload
    if(data.getName()==null)
       throw new InvalidDataException("Name shouldnt be null");
}

liwlm1x9

liwlm1x91#

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

private ResponseEntity<String> exchange(String url, HttpMethod method, HttpEntity<?> httpEntity,
                                           Class<?> class1, Map<String, String> paramMap) {
        ResponseEntity<String> responseEntity = null;
        try {
            responseEntity = restTemplate.exchange(url, method, httpEntity, String.class, paramMap);
        }catch(HttpClientErrorException e) {
            responseEntity = new ResponseEntity<>(e.getResponseBodyAsString(), HttpStatus.BAD_REQUEST);
        }catch(HttpServerErrorException e) {
            responseEntity = new ResponseEntity<>(e.getResponseBodyAsString(), HttpStatus.INTERNAL_SERVER_ERROR);
            throw e;
        }catch(Exception e) {
            responseEntity = new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            throw e;
        }
        return responseEntity;
    }

字符串

oogrdqng

oogrdqng2#

您缺少:

headers.setContentType(MediaType.APPLICATION_JSON);

字符串

rnmwe5a2

rnmwe5a23#

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

public class MyErrorHandler extends DefaultResponseErrorHandler {

    @Override
    protected void handleError(ClientHttpResponse response, HttpStatusCode statusCode) throws IOException {

    }

}

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

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


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

相关问题