In a Spring example, how is a Validator object generated?
I am studying a spring exmaple. And I found the following code. I can't
understand the construct function. A validator interface is passed to this
function. How is this validator generated? Thanks in advance.
@Controller
@RequestMapping(value="/account")
public class AccountController {
private Map<Long, Account> accounts = new ConcurrentHashMap<Long,
Account>();
private Validator validator;
@Autowired
public AccountController(Validator validator) {
this.validator = validator;
}
@RequestMapping(method=RequestMethod.POST)
public @ResponseBody Map<String, ? extends Object> create(@RequestBody
Account account, HttpServletResponse response) {
Set<ConstraintViolation<Account>> failures = validator.validate(account);
if (!failures.isEmpty()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return validationMessages(failures);
} else {
accounts.put(account.assignId(), account);
return Collections.singletonMap("id", account.getId());
}
}
No comments:
Post a Comment