On my Spring Boot RestController I want to pass a custom error message to the response body by throwing a custom exception. I was following the guide on https://dzone.com/articles/spring-rest-service-exception-handling-1
These are my code Snippets:
UserNotFoundException
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
}
}
The simplified RestController
@RestController
public class CreateRfqController {
@PostMapping("api/create-rfq")
public ResponseEntity<Rfq> newRfq(@RequestBody Rfq newRfq) {
throw new UserNotFoundException("User Not Found");
}
}
When calling my Rest API I always get an empty message:
{
"timestamp": "2020-06-24T18:23:30.846+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/api/create-rfq"
}
instead i want "message": "User Not Found",
At one Stage I even had it working. Seems like i messed up on some Point.
In the log you can even see the User Not Found text and the correct Exception Class
24 Jun 2020;20:50:52.998 DEBUG = 145: o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolved [configRestServer.exceptions.UserNotFoundException: User Not Found]
24 Jun 2020;20:50:52.998 DEBUG = 1131: o.s.w.s.DispatcherServlet - Completed 404 NOT_FOUND
24 Jun 2020;20:50:53.003 DEBUG = 91: o.s.w.s.DispatcherServlet - "ERROR" dispatch for POST "/error", parameters={}
24 Jun 2020;20:50:53.006 DEBUG = 414: o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
24 Jun 2020;20:50:53.018 DEBUG = 265: o.s.w.s.m.m.a.HttpEntityMethodProcessor - Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
24 Jun 2020;20:50:53.018 DEBUG = 91: o.s.w.s.m.m.a.HttpEntityMethodProcessor - Writing [{timestamp=Wed Jun 24 20:50:53 CEST 2020, status=404, error=Not Found, message=, path=/api/create-rf (truncated)...]
24 Jun 2020;20:50:53.038 DEBUG = 1127: o.s.w.s.DispatcherServlet - Exiting from "ERROR" dispatch, status 404
my build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.3.0.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.1.RELEASE'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
runtime group: 'com.oracle', name: 'ojdbc6', version: '11.2.0.3'
compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.12'
Update: On Purpose I passed some wrong variable type into my RFQ Object to produce a default error. Even here in the response, the message is empty:
{
"timestamp": "2020-06-24T19:20:18.831+00:00",
"status": 403,
"error": "Forbidden",
"message": "",
"path": "/api/create-rfq"
}
The most basic way of returning an error message from a REST API is to use the @ResponseStatus annotation. We can add the error message in the annotation's reason field. Although we can only return a generic error message, we can specify exception-specific error messages.
The differences between @RestControllerAdvice and @ControllerAdvice is : @RestControllerAdvice = @ControllerAdvice + @ResponseBody . - we can use in REST web services. @ControllerAdvice - We can use in both MVC and Rest web services, need to provide the ResponseBody if we use this in Rest web services.
Spring RestController annotation is used to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON or XML response.
Exception HandlerThe @ExceptionHandler is an annotation used to handle the specific exceptions and sending the custom responses to the client. Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown.
It is changed behavior in Spring Boot 2.3.0. Change the property server.error.include-message = always in your application.properties/yml file.
More informations: Changes to the Default Error Page’s Content and Spring Boot server properties
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With