I use next: java11 + spring-boot:2.4.3.
I have next method in controller:
@PutMapping("/{id}")
@ResponseBody
public void update(@PathVariable("id") long id, @RequestBody OrderDto orderDto) {
try {
myService.update(id, orderDto);
} catch (OrderMovementException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getLocalizedMessage(), e);
}
}
Well, it works fine for tests or logging, I see status and message.
But my front-end colleague receive next, where message is empty:
{"timestamp":"2022-01-24T11:20:48.726+00:00","status":400,"error":"Bad Request","message":"","path":"/service/api/orders/12345"}
what I need to change to fix this problem? Thanks in advance.
Your method could instead or returning void return class ResponseEntity
. See an article on how to use it: Using Spring ResponseEntity to Manipulate the HTTP Response. Another way to provide detailed error message is to write a class annotated with @ControllerAdvice
. See some info on that (and other stuff) here: Building REST services with Spring