I am developing a small REST API by using Spring Boot. I am using validation annotations in my entity class:
@NotEmpty(message="Please enter a first name")
private String firstName; 
Here is my controller and service:
@RestController
public class CustomerController {
    @Autowired
    private CustomerService userService;
    @PostMapping("/customer")
    @ResponseStatus(HttpStatus.CREATED)
    public Customer postUser (@RequestBody @Valid Customer customer){
      return userService.save(customer);
    } 
}
public class CustomerService {
    @Autowired
    private CustomerRepository repository;
    @Transactional
    public Customer save ( Customer customer){
        return repository.save(customer);
    }
}
Here is the response I get when I try to send a JSON with an empty firstName Value:
{
    "timestamp": "2020-06-08T08:40:08.660+00:00",
    "status": 400,
    "error": "Bad Request",
    "message": "",
    "path": "/MVCwebshop/customer"
}
As you can see, the "message" field is empty in the JSON I get. This is also happening when I try to use the default messages. How can I fix this?
In case of using Spring Boot 2.3.0.RELEASE you have to set
server.error.include-message=always
in your application.properties (or yaml).
See here
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