【发布时间】:2016-06-18 16:41:53
【问题描述】:
我正在使用 mybatis 来执行所有数据库操作。我还在使用 Angular 前端,因此客户端中的验证是使用 angular-validation-ghiscoding 和原生 HTML5 验证进行的。我想验证银行端的数据到,但我不想使用注解。
下面是代码示例:
@RequestMapping(value = SecureApiResources.URI_UPDATE_ACCOUNT, method = RequestMethod.POST, produces = "application/json")
public @ResponseBody Account updateAccount(
@RequestBody final AccountRequestUpdate accountRequest) { // Object to be validated (accountRequest)
Account account = accountMapper.getAccountOfMerchant(authContextHolder.getMerchantId(), authContextHolder.getDefaultAccountId());
if (account == null) {
HttpErrors httpErrors = new HttpErrors(
SecureApiResources.ERROR_ACCOUNTS_NOT_FOUND);
throw new EntityNotFoundException(httpErrors);
}
int resultUpdate;
try {
// In this point I should validate the accountRequest object
account.setAccountName(accountRequest.getAccountName());
account.setCommercialName(accountRequest.getCommerciaName());
account.setCountry(accountRequest.getCountry());
account.setCity(accountRequest.getCity());
resultUpdate = accountMapper.updateMerchant(account);
if (resultUpdate == 0) {
HttpErrors httpErrors = new HttpErrors(
SecureApiResources.ERROR_ACCOUNTS_NOT_FOUND);
throw new EntityNotFoundException(httpErrors);
}
} catch (Exception e) {
HttpErrors httpErrors = new HttpErrors(
SecureApiResources.ERROR_SQL_NOT_EXECUTE);
throw new EntityNotFoundException(httpErrors);
}
return account;
}
在同一个类中,我有一个创建帐户的方法,我收到了另一个模型对象 (AccountRequestCreate accountRequest)。
在没有 xml 和注解的情况下,最推荐的选项是哪个?
【问题讨论】:
-
讽刺的是,最推荐的方法是使用
@Valid或@validated注释!注释有什么问题?您已经添加了一个@RequestBody,我想您可以为更多注释腾出空间。
标签: java spring validation spring-mvc bean-validation