Spring MVC - RestTemplate launch exception when http 404 happens

前端 未结 5 1260
野性不改
野性不改 2020-12-30 19:48

I have a rest service which send an 404 error when the resources is not found. Here the source of my controller and the exception which send Http 404.

@Contr         


        
5条回答
  •  抹茶落季
    2020-12-30 20:26

    As far as I'm aware, you can't get an actual ResponseEntity, but the status code and body (if any) can be obtained from the exception:

    try {
        ResponseEntity r = restTemplate.getForEntity(url, StoreDto.class, m);
    }
    catch (final HttpClientErrorException e) {
        System.out.println(e.getStatusCode());
        System.out.println(e.getResponseBodyAsString());
    }
    

提交回复
热议问题