@NotNull : validation custom message not displaying
Try this.
@ControllerAdvice
public class RestExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleAllExceptionMethod(Exception ex, WebRequest requset) {
ExceptionMessage exceptionMessageObj = new ExceptionMessage();
// Handle All Field Validation Errors
if(ex instanceof MethodArgumentNotValidException) {
StringBuilder sb = new StringBuilder();
List<FieldError> fieldErrors = ((MethodArgumentNotValidException) ex).getBindingResult().getFieldErrors();
for(FieldError fieldError: fieldErrors){
sb.append(fieldError.getDefaultMessage());
sb.append(";");
}
exceptionMessageObj.setMessage(sb.toString());
}else{
exceptionMessageObj.setMessage(ex.getLocalizedMessage());
}
exceptionMessageObj.setError(ex.getClass().getCanonicalName());
exceptionMessageObj.setPath(((ServletWebRequest) requset).getRequest().getServletPath());
// return exceptionMessageObj;
return new ResponseEntity<>(exceptionMessageObj, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@NotNull and @size Spring MVC Validation does not work and does not display the custom message
you have to add this below in the pom.xml
Bean Validation API 2.0.1:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
Hibernate Validator 7.0.1 Final:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.1.Final</version>
</dependency>
Then you have to update the maven.
This post also helps you to find a more descriptive answer. @NotNull annotation is not working in Spring boot application
If you don't have a pom.xml,
Add jar files to your lib
folder. But exactly your newly added jars, you can see in external libraries folder. Using these links, you can download hibernate-validator.jar and validation-api-2.0.1.final.jar.
This post may help you to add these jar files to lib folder Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project
Entity not null validation message
You need to perform few steps to achieve custom message:
@Column
@NotNull(message = "error.title.notnull")
@Size(min = 1, max = 30, message = "error.title.size")
private String title;
@Column
@Size(max = 100, message = "error.description.size")
private String description;
So, the next step is to make a MessageSource bean by adding one to SpringValidationApplication (the main class).
For example:
@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageBundle = new ReloadableResourceBundleMessageSource();
messageBundle.setBasename("classpath:messages/messages");
messageBundle.setDefaultEncoding("UTF-8");
return messageBundle;
}
This tells Spring to look at the messages folder for files starting with messages.
Next add a file called messages.properties
inside src/main/resources/messages:
error.title.notnull=The title is a required field
error.title.size=The title should be between 1 and 30 characters
error.description.size=The description should be limited to 100 characters.
Detail can be found on this link
Given error message is not displayed in Spring Form validation
As per your current code you'll get username cannot be empty
only if username is null. Change it to @NotEmpty
. And you'll get e mail cannot be empty
only if the email formatting is incorrect. If email is empty, the default message will be printed because you haven't attached any message to @NotEmpty
of email property. @Sanjay's suggestion will fix this.
Related Topics
Alternative to If-Else Statements or Better Approach
Getting Java.Lang.Classnotfoundexception: Javax.Servlet.Servletcontext in Junit
Spring Jpa/Hibernate Transaction Force Insert Instead of Update
How to Find Out If String Has Already Been Url Encoded
Disable Spring Security Config Class for @Webmvctest in Spring Boot
I Get a Status 200 When Connecting to the Websocket, But It Is an Error
How to Pass Variables Between Cucumber-Jvm Steps
How to Sort Arraylist of Objects by Timestamp and Get Last Five Elements
@Notnull:Validation Custom Message Not Displaying
How to Solve Gradle Error Cannot Find Symbol
Spring Kafka - How to Reset Offset to Latest With a Group Id
Gradle Java9 Could Not Target Platform: 'Java Se 9' Using Tool Chain: 'Jdk 8 (1.8)'
Adding a Custom Http Header to a Spring Boot Ws Call (Wstemplate)
How to Change the Background Color of a Textfield Without Changing the Border in Javafx
Custom Pagination by Array List
How to Refresh or Reload the Jframe