title: Having issue regarding validator in spring boot
body: <p>I am developing project in spring boot and i am using validator to validate user.</p>
  
  <pre><code>    import java.io.IOException;
  import java.util.Map;
  import java.util.Properties;
  
  import javax.annotation.PostConstruct;
  import javax.servlet.http.HttpServletResponse;
  import javax.validation.Valid;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.web.bind.WebDataBinder;
  import org.springframework.web.bind.annotation.InitBinder;
  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.fasterxml.jackson.databind.ObjectMapper;
  import com.newDemo.common.service.ResourceProperties;
  import com.newDemo.constants.Constant;
  import com.newDemo.modules.user.domain.AuthenticationToken;
  import com.newDemo.modules.user.domain.User;
  import com.newDemo.modules.user.service.AuthenticationTokenService;
  import com.newDemo.modules.user.service.SocialAuthService;
  import com.newDemo.modules.user.service.UserService;
  import com.newDemo.modules.user.validator.UserValidator;
  import com.newDemo.utils.ResponseHandlerUtil;
  
  @RestController
  @RequestMapping("/api/v1/user")
  public class UserController {
  
      public static Logger log = LoggerFactory.getLogger(UserController.class);
      @Autowired UserService userService;
      @Autowired AuthenticationTokenService authenticationTokenService;
      @Autowired SocialAuthService socialAuthService;
      @Autowired ResourceProperties resourceProperties;
      private Properties configProp;
  
      /**
       * load message property file
       * @throws IOException
       */
      @PostConstruct
      public void init() throws IOException{
          configProp = resourceProperties.getResourceProperties(Constant.RESPONSE_MSG_FILE_PATH);
      }
  
      @InitBinder
      protected void initBinder(WebDataBinder binder) {
          binder.setValidator(new UserValidator());
      }
  
      /**
       * this method used to create new user or update user
       * @author oodles
       * @return
       */
      @RequestMapping(method = RequestMethod.POST)
      public Map&lt;String, Object&gt; addUser(@Valid @RequestBody User user) {
          log.debug("adding new user");
          return ResponseHandlerUtil.generateResponse(
                  configProp.getProperty("user.created"), HttpStatus.ACCEPTED,
                  true, userService.addUser(user));
      }
  }
  </code></pre>
  
  <p>This method is working fine . It validates the user when i call this method.
  But when i call another method in same controller it gives me exception.</p>
  
  <pre><code>java.lang.IllegalStateException: Invalid target for Validator [com.newDemo.modules.user.validator.UserValidator@6582b808]: {social_auth_token=CAACEdEose0cBAGXgjErY8fPJQNqPzSpMoU2FagZBLgOo4P7xtpoJgWSEOAINrmj4HTBuhUKpO5jQBYTH3z2jq92kog90R1qzXBKmZC7OEEXN8gaZBuNqZBYN9iPSoKLL9ZAcTgojRkcz2dQjcJTAhvq7NG8PoK9ZAvShYKN5QOmimI5ZALNMxqasFBZBaZBHUKb4yBClcWA9AUMWdzDQYRWqnZAsIrmhN78zcZD, login_type=facebook, userdetails={socialId=661252653921786, email=qsiddiqui81@gmail.com, username=qsiddiqui81@gmail.com, age=20, firstName=Qasim, lastName=Siddiqui, gender=male, address={city=gurgaon, state=Haryana}}}
      at org.springframework.validation.DataBinder.assertValidators(DataBinder.java:516)
      at org.springframework.validation.DataBinder.setValidator(DataBinder.java:507)
      at com.newDemo.modules.user.controller.UserController$$EPJF5LJA.initBinder(UserController.java:55)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  </code></pre>
  
  <p>Second method which gives exception in this controller is </p>
  
  <pre><code> @RequestMapping(value = "/sociallogin", method = RequestMethod.POST)
          public Map&lt;String, Object&gt; login(@RequestBody Map map,HttpServletResponse resp) throws IOException{
                  Map&lt;String, Object&gt; data = null;
                  final ObjectMapper mapper = new ObjectMapper();
  User user = (User)mapper.readValue(mapper.writeValueAsString(map.get("userdetails")), User.class);
                  data = socialAuthService.login((String) map.get("social_auth_token"), (String) map.get("login_type"), user);
              return ResponseHandlerUtil.generateResponse(configProp.getProperty("login.success"),HttpStatus.ACCEPTED, true, data);
          }
  </code></pre>
  
  <p>I do not want validation over my second method but when i make hit over second method it gives me above specified exception.
  And my second question is in future if i want to validate over user(fetching user from map using object mapper) in second method using validator then how to do that?</p>
  
  <p>My validator class is as follows</p>
  
  <pre><code>package com.newDemo.modules.user.validator;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.validation.Errors;
  import org.springframework.validation.ValidationUtils;
  import org.springframework.validation.Validator;
  
  import com.newDemo.modules.user.domain.User;
  
  public class UserValidator implements Validator{
  
      public static Logger log = LoggerFactory.getLogger(UserValidator.class);
      @Override
      public boolean supports(Class&lt;?&gt; clazz) {
          // TODO Auto-generated method stub
          return User.class.equals(clazz);
      }
  
      @Override
      public void validate(Object target, Errors err) {
          // TODO Auto-generated method stub
          log.debug("validating user");
          ValidationUtils.rejectIfEmptyOrWhitespace(err, "email", "email.empty");
          ValidationUtils.rejectIfEmptyOrWhitespace(err, "username", "username.empty");
          ValidationUtils.rejectIfEmptyOrWhitespace(err, "password", "password.empty");
          ValidationUtils.rejectIfEmptyOrWhitespace(err, "firstName", "firstName.empty");
          ValidationUtils.rejectIfEmptyOrWhitespace(err, "gender", "gender.empty");
      }
  
  }
  </code></pre>