I'm trying to throw a custom exception when Resource is not found, somehow the custom exception is overridden by the SpringFramework and custom message is not shown. below is the a custom exception I have written
@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class SkeletonNotFoundException extends RuntimeException {
public SkeletonNotFoundException(String message) {
super(message);
}
}
I want to throw above exception when Mono not found, below is the logic of finding A resource and throwing the error
@GetMapping(value = "/{skeletonId}")
public Mono<ResponseEntity<?>> getSkeleton(@PathVariable final Long skeletonId) {
return skeletonService.findById(skeletonId)
.switchIfEmpty(Mono.error(new SkeletonNotFoundException("Skeleton not found")))
.map(skeleton -> this.skeletonMapper.mapToDto(skeleton))
.map(body -> ResponseEntity.ok().body(body));
}
Below is the result i get from postman when I send request to the endpoint
{
"timestamp": "2020-11-29T06:15:26.935+00:00",
"path": "/api/v1/skeletons/3",
"status": 404,
"error": "Not Found",
"message": "",
"requestId": "2094d9b0-1"
}
I can't see my custom message, somehow another layer is overriding the CustomException
ErrorWebExceptionHandler. In that way, you will be sure the returned404is related or not with such exception. Several examples about how to do it: baeldung.com/spring-webflux-errors and stackoverflow.com/questions/47958622/… - doctoreWebExceptionHandleris only needed if you need to handle exceptions that are thrown before a handler is chosen, so for instance exceptions that are thrown in WebFilters. Which is clearly pointed out in the stack overflow linked example. - Toerktumlare@RestControllerAdviceis clear the problem is in "other part". Not sure the Webflux version you are using, there was a problem related with it: github.com/spring-projects/spring-boot/pull/19901 I just created a dummy project with the latest one, returningMono.empty()inskeletonService.findById(skeletonId)and it works as expected, that is,Skeleton not foundwas included in the responsemessageproperty. - doctore