@RestController
public class ErrorServlet {
@GetMapping
public void get() {
throw HttpClientErrorException.create(HttpStatus.LOCKED, "status should be locked", null, null, null);
}
}
Expected: a 423 http status should be returned.
Current result:
{
"timestamp": "2020-03-09T14:53:41.812+0000",
"status": 500,
"error": "Internal Server Error",
"message": "423 status should be locked",
"path": "/"
}
spring-boot.2.2.5.RELEASE
Comment From: rstoyanchev
I presume you're not actually throwing that exception but it's coming as a result of using the RestTemplate? If you are actually throwing it, then please use ResponseStatusException instead. If it is from the RestTemplate we can't automatically assume how to treat it. You'll need to adapt it with @ExceptionHandler.
Comment From: membersound
Actually I'm creating the exception myself. I did not know it should not be created programmatically (but then the question is: why is the class and creator public?).
So the correct exception for custom errors is ResponseStatusException?
Comment From: rstoyanchev
Yes, see https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-exceptionhandlers, in particular ResponseStatusExceptionHandler.
HttpClientErrorException is part of the public API for the RestTemplate and it's perfectly okay to create it for example from within a ResponseErrorHandler or a ClientHttpRequestInterceptor.