Search code examples
javaspringresthttpexception

When HttpStatusCodeException exception raised?


when i use below code , what is the case to get HttpStatusCodeException exception .

ResponseEntity<Object> response = 
  restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, entity, Object.class);

Please can anyone help out ????????


Solution

  • HTTP Status Codes are responses from the server, therefore if you have control of the server then you could make it return whichever errors you want. If you don't have control of the server then you could try sending bad/invalid requests so that the server will complain.

    Something like this on your server side:

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity getAnError() {
        // complain!
        return ResponseEntity.status(HttpStatus.FORBIDDEN);
    }