I'm creating a simple Spring REST application and following is my spring configuration class;
@Configuration
@EnableWebMvc
@ComponentScan( basePackages = "com.example.spring" )
public class AppConfig {
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/messages");
return messageSource;
}
}
And this is what i have done in my controller class;
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ResponseEntity<Map<String, String>> createEmployee(@Valid @RequestBody EmployeeModel employeeModel, BindingResult result) {
if (result.hasErrors()) {
System.out.println("Errors: " + result.getAllErrors());
Map<String, String> response = new HashMap<>();
response.put("status", "failure");
return new ResponseEntity<>(response, HttpStatus.OK);
}
employeeService.createEmployee(employeeModel);
Map<String, String> response = new HashMap<>();
response.put("status", "success");
return new ResponseEntity<>(response, HttpStatus.OK);
}
I have annotated my name field in EmployeeModel as below;
@NotEmpty
@Column(name = "name")
private String name;
And this is my messages.properties file which is in the src/main/resources folder.
NotEmpty.employeeModel.name = Employee name is required.
But i'm not able to read this error message i'm getting only the default message for @NotEmpty.
I even tried with the following configuration;
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("classpath:/messages");
return messageSource;
}
Kindly help me with this!