Spring Bean Custom Validation for Mobile Apps – JSR-303 Annotations

DZone 's Guide to

Spring Bean Custom Validation for Mobile Apps – JSR-303 Annotations

This article shows you how to perform Spring Bean custom validations for mobile apps with JSR-303 annotations.

· Mobile Zone ·
Free Resource

This article shows you how to perform Spring Bean custom validations for mobile apps with JSR-303 annotations.

1. Below dependencies should be added to pom.xml to handle spring bean validations:

<!--Java validation jars -->



<!--Hibernate validation jars -->


2. Provide below hibernate validations to your DTO (in this example MyDTO. java):

public class MyDTO {


    @Size(min = 1, max = 16)

    @Pattern(regexp = "[a-zA-Z0-9]+")

    private String name;


    @Size(min = 1, max = 12)

    private String password;


3. messageSource definition should be provided to context.xml(src\main\webapp\WEB-INF\applicationContext.xml). This definition will intialize the messages.properties file:

<bean id="messageSource"     class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">

4. Below validation messages should be provided to messages.properties file:

4. Below validation messages should be provied to messages.properties file (src\main\resources\messages.properties) 

NotEmpty ={0} cannot be empty.
Size ={0} should be between {2} and {1} characters.
Pattern = The {0} must be a alphanumeric.

5. Add below definition to your controller to handle validation for DTO object (MyController.java):

import javax.validation.Valid;

public class MyController {

    @RequestMapping(value = "/v1/sessions/login", method = RequestMethod.POST)
    public ResponseEntity<LoginDTO> login(@Valid @RequestBody MyDTO login)
    throws Exception {
        //code goes here



6. Added below Exception handler to your controller advice (MyControllerAdvice.java):

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.mypackage.restservices.model.RestErrors;

import com.mypackage.restservices.model.Errors;

public class MyControllerAdvice {

    private MessageSource messageSource;

    protected ResponseEntity<RestErrors>     handleValidationException(MethodArgumentNotValidException ex) {
        List<Errors> errors = new ArrayList<Errors>();
        List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            Errors error = new Errors();
            error.setDefaultMessage(messageSource.getMessage(fieldError, null));
        RestErrors restErrors = new RestErrors("Validation Failed", errors);
        return new ResponseEntity<RestErrors>(restErrors, HttpStatus.BAD_REQUEST);


7. RestErrors.java changes to display properly to the user:

package com.mypackage.restservices.model;
import java.util.List;
public class RestErrors {

    private String message;
    private List<Errors> errors;
    public RestErrors(String message) {
        this.message = message;
    public RestErrors(String message, List<Errors> errors) {
        this.message = message;
        this.errors = errors;
    public void setMessage(String message) {
        this.message = message;
    public String getMessage() {
        return message;
    public void setErrors(List<Errors> errors) {
        this.errors = errors;
    public List<Errors> getErrors() {
        return errors;

8. Errors.java changes to store error code and default message to be displayed in the UI:

package com.mypackage.restservices.model;

public class Errors {
    private String code;
    private String defaultMessage;
    public String getCode() {
        return code;
    public void setCode(String code) {
        this.code = code;
    public String getDefaultMessage() {
        return defaultMessage;
    public void setDefaultMessage(String defaultMessage) {
        this.defaultMessage = defaultMessage;

bean validation api support ,mobile app ,spring validation

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}