@Notnull:Validation Custom Message Not Displaying

@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



Leave a reply



Submit