How to send a custom validation Json object for the requestbody?

Jackson Baby

How can I send a custom JSON error instead of the one provided by spring? I need both custom validation and default validation like @NotNull, @NotEmpty, @Email and so on.

The function is only fired if the validation constraints in the POJO class level is not violated

Code

package com.flasher.controller.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.flasher.dto.UserRegistrationInfo;
import com.flasher.service.RegistrationService;
import com.flasher.validator.UserRegistrationInfoValidator;

@RestController
@RequestMapping(value = "/api")
public class RegistrationControllerRest {

    @Autowired
    RegistrationService registrationService;

    @Autowired
    UserRegistrationInfoValidator userRegistrationInfoValidator;

    @RequestMapping(value = "/v1/user", method = RequestMethod.POST)
    public void registerUser(@RequestBody @Valid UserRegistrationInfo userRegistrationInfo, HttpServletRequest request,
            BindingResult bindingResult) {
        userRegistrationInfoValidator.setRemoteAddr(request.getRemoteAddr());
        userRegistrationInfoValidator.validate(userRegistrationInfo, bindingResult);
        if(bindingResult.hasErrors()){
            System.out.println("has errors--");
            List<FieldError>   fieldErrors =bindingResult.getFieldErrors();
            fieldErrors.stream().forEach(fieldError->{
                System.out.println(fieldError.getField()+" "+fieldError.getDefaultMessage());
            });
        }

    }

}


package com.flasher.dto;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

public class UserRegistrationInfo {
    @NotNull
    @NotEmpty
    @Length(min = 4, max = 30)
    private String userName;
    @NotNull
    @NotEmpty
    @Email
    private String email;
    @NotNull
    @NotEmpty
    @Length(min = 4, max = 15)
    private String password;
    @NotNull
    @NotEmpty
    private String reCaptcha;
    @NotNull
    private Boolean isAgreedTerms;

    public UserRegistrationInfo() {
    }

    public UserRegistrationInfo(String userName, String email, String password, String reCaptcha,
            Boolean isAgreedTerms) {
        this.userName = userName;
        this.email = email;
        this.password = password;
        this.reCaptcha = reCaptcha;
        this.isAgreedTerms = isAgreedTerms;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getReCaptcha() {
        return reCaptcha;
    }

    public void setReCaptcha(String reCaptcha) {
        this.reCaptcha = reCaptcha;
    }

    public Boolean getIsAgreedTerms() {
        return isAgreedTerms;
    }

    public void setIsAgreedTerms(Boolean isAgreedTerms) {
        this.isAgreedTerms = isAgreedTerms;
    }

}



package com.flasher.validator;

import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.flasher.dto.UserRegistrationInfo;
import com.flasher.util.GoogleReCaptchaChecker;

public class UserRegistrationInfoValidator implements Validator {
    @Autowired
    GoogleReCaptchaChecker googleReCaptchaChecker;
    String remoteAddr;
    @Override
    public boolean supports(Class<?> className) {

        return UserRegistrationInfo.class.equals(className);
    }

    @Override
    public void validate(Object obj, Errors errs) {
        UserRegistrationInfo userRegistrationInfo = (UserRegistrationInfo) obj;

        if(Pattern.compile("[^\\w]").matcher(userRegistrationInfo.getUserName()).find()){
            errs.rejectValue("userName","", "Username cannot contain special characters");  
        }   
        if(Pattern.compile("[^(\\w!@#$%^&*())]").matcher(userRegistrationInfo.getUserName()).find()){
            errs.rejectValue("userName","", "Password cannot contain disallowed characters");   
        }
        if(!userRegistrationInfo.getIsAgreedTerms()){
            errs.rejectValue("isAgreedTerms", "you have to agree the terms and condition  in order to register");   
        }
        if(!googleReCaptchaChecker.validateGoogleReCaptcha(userRegistrationInfo.getReCaptcha(),remoteAddr)){
            errs.rejectValue("reCaptcha", "","invalid reCaptcha");  

        }

    }

    public void setRemoteAddr(String remoteAddr) {
        this.remoteAddr = remoteAddr;
    }

}

Responce

{
  "timestamp": 1487250555542,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.bind.MethodArgumentNotValidException",
  "errors": [
    {
      "codes": [
        "Length.userRegistrationInfo.userName",
        "Length.userName",
        "Length.java.lang.String",
        "Length"
      ],
      "arguments": [
        {
          "codes": [
            "userRegistrationInfo.userName",
            "userName"
          ],
          "arguments": null,
          "defaultMessage": "userName",
          "code": "userName"
        },
        30,
        4
      ],
      "defaultMessage": "length must be between 4 and 30",
      "objectName": "userRegistrationInfo",
      "field": "userName",
      "rejectedValue": "",
      "bindingFailure": false,
      "code": "Length"
    },
    {
      "codes": [
        "NotEmpty.userRegistrationInfo.userName",
        "NotEmpty.userName",
        "NotEmpty.java.lang.String",
        "NotEmpty"
      ],
      "arguments": [
        {
          "codes": [
            "userRegistrationInfo.userName",
            "userName"
          ],
          "arguments": null,
          "defaultMessage": "userName",
          "code": "userName"
        }
      ],
      "defaultMessage": "may not be empty",
      "objectName": "userRegistrationInfo",
      "field": "userName",
      "rejectedValue": "",
      "bindingFailure": false,
      "code": "NotEmpty"
    }
  ],
  "message": "Validation failed for object='userRegistrationInfo'. Error count: 2",
  "path": "//api/v1/user"
}
Jackson Baby
@ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ValidationErrorResponse handleMathodArgumentNotValidException(
            MethodArgumentNotValidException methodArgumentNotValidException) {

        BindingResult bindingResult = methodArgumentNotValidException.getBindingResult();
        List<ValidationError> validationErrors = new ArrayList<ValidationError>();
        if (bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            fieldErrors.stream().forEach(fieldError -> {
                validationErrors.add(new ValidationError(fieldError.getField(), fieldError.getDefaultMessage()));
            });

        }
        return new ValidationErrorResponse(validationErrors);
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Validation of JSON Object With @Valid and @Requestbody in SpringBoot

how to receive a json object with @RequestBody or @RequestParam

@RequestBody not parsing request into object for validation

Spring @RequestBody not mapping to custom Object

Bean Validation transforms Object RequestParam into @RequestBody

How to send a JSON object to a servlet

How to send Custom Object to Kafka Topic with Producer

How do I enable strict validation of JSON / Jackson @RequestBody in Spring Boot REST API?

How to send JSON response object from custom class to another activity android

How to send Multiple parameters in passes function of custom validation rule

How to send JSON object as JSON String with Postman?

How to send JSON from postman for a custom VO

How to send @Requestbody and @Requestpart together in spring

@RequestBody not mapping JSON to Java Object - Spring Boot

send custom RequestBody from POST request to an external GET request in java

How to send json object to razor model then angularjs

How to send JS object as JSON correct?

How to send a mock object as JSON in mockmvc

How to send form data as a single JSON object?

how to send json object in ajax to a php page

How to send json object as post request in CRONET?

How to send JSON object to a component in React?

How to send JSON object from Perl to Python?

How to send json object to the Controller JsonResult Method

How to send JSON object with ArrayBuffer to websocket?

How to custom convert String to enum in @RequestBody?

How to read additional JSON attribute(s) which is not mapped to a @RequestBody model object in Spring boot

How to pass entity object to controller from custom validation annotation

How to trigger custom annotated validation rule of an object manually?