I\'m having a controller like this one (in Kotlin):
@RestController
@RequestMapping(\"/\")
class CustomerController (private val service: CustomerService) {
You can use ResponseStatusException, just extend your exception:
public class YourLogicException extends ResponseStatusException {
public YourLogicException(String message) {
super(HttpStatus.NOT_FOUND, message);
}
public YourLogicException(String message, Throwable cause) {
super(HttpStatus.NOT_FOUND, message, cause);
}
And in service:
public Mono doLogic(Mono ctx) {
return ctx.map(ctx -> doSomething(ctx));
}
private String doSomething(YourContext ctx) {
try {
// some logic
} catch (Exception e) {
throw new YourLogicException("Exception message", e);
}
}
And after that, you could have a pretty message:
{ "timestamp": 00000000, "path": "/endpoint", "status": 404, "error": "Not found", "message": "Exception message" }