I have a controller that receives as @RequestBody a string containing a request XML.
@RequestMapping(method = RequestMethod.POST, value="/app-authorization")
public Response getAppAuthorization(
HttpServletResponse response, BindingResult results,
@RequestBody String body){
log.info("Requested app-authorization through xml: \n" + body);
Source source = new StreamSource(new StringReader(body));
RefreshTokenRequest req = (RefreshTokenRequest) jaxb2Mashaller.unmarshal(source);
validator.validate(req, results);
if(results.hasErrors()){
log.info("DOESN'T WORK!");
response.setStatus(500);
return null;
}
InternalMessage<Integer, Response> auth = authService.getRefreshToken(req);
response.setStatus(auth.getHttpStatus());
return auth.getReponse();
}
The AuthorizationValidator is the following:
@Component("authenticationValidator")
public class AuthenticationValidator implements Validator{
public boolean supports(Class<?> clazz) {
return AuthorizationRequest.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
errors.rejectValue("test", "there are some errors");
}
}
I'd like to know if there's a way to inject an object Validator into my controller in such a way that:
- @Autowired \n Validator validator; makes me obtain automatically a reference to my AuthenticationValidator
- every controller is linked to one or more validators withouth indicating their class explicitly.