Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Rest Api not displaying validation message

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?

like image 924
efthimisVaf Avatar asked Jun 08 '20 09:06

efthimisVaf


1 Answers

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

like image 61
Lars Michaelis Avatar answered Oct 04 '22 04:10

Lars Michaelis