1

I'm using @Restcontroller and @Vaid annotation.

    @RequestMapping(path = "/1050"
    method = RequestMethod.POST,
            headers = {"Content-Type=application/json"},
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE

)
        public UserListResp getUserList(@RequestBody @Valid UserListReq request, BindingResult bindingResult,
                                        Principal principal){

     UserListResp response = new UserListResp();

     if (bindingResult.hasErrors()){


            response.setResultCode(102); // Validation error
            response.setErrMsg("Wrong " + bindingResult.getFieldError().getDefaultMessage() + " value.");

        } else {
            return userService.getUserList(request) ;
        }

            return response;   
    }

Incoming request mapped to object which validated.

public class UserListReq {

   private String userName;
   .... 
}

I'm not getting this value (userName) from incoming json request, I've got from oAuth service by token. Is it possible to send userName to validation constraint from @ControllerAdvice ?

@InitBinder
    public void dataBinding(WebDataBinder binder, HttpServletRequest req) {

        // userName
        req.getUserPrincipal().getName();
    }

Thanks.

koa73
  • 861
  • 2
  • 10
  • 27
  • check http://stackoverflow.com/questions/14732598/spring-mvc-process-object-before-valid-is-applied – kuhajeyan Oct 18 '16 at 08:19
  • Thanks But my problem more difficult. I have rest controller and I've sent json in body request ( I'm not using POST or GET form parameters ). – koa73 Oct 18 '16 at 08:25

1 Answers1

0

I've found decision

@ControllerAdvice
public class GlobalControllerAdvice {


    @InitBinder
    public void dataBinding(WebDataBinder binder, HttpServletRequest request) {

        binder.bind(new MutablePropertyValues(Collections.singletonMap("userName",request.getUserPrincipal().getName())));
    }

}
koa73
  • 861
  • 2
  • 10
  • 27