我正在使用 spring (4.2.0.RELEASE)、hibernate 验证器 (5.2.1.Final) 和验证 api (1.1.0.Final) 为后端应用程序使用以下配置进行 JSR 验证,
<bean id="validatorFactory" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource" />
</bean>
<bean class="org.springframework.expression.spel.standard.SpelExpressionParser" />
但没有一个 JSR 303 注释在我的应用程序中工作。
注意:在 POJO 类上添加了 jsr 303 注释,在服务类(使用 POJO)上添加了 @Validated 也尝试在方法级别添加 @Validated。
更新:服务接口
@Validated
public interface SampleService {
@NotNull
@Valid
Account getAccount( @NotNull @Valid String customerKey, @NotNull String name);
服务实施
@Service
@Validated
@Transactional( readOnly=true )
public class SampleServiceImpl
implements SampleService
{
private final SampleDao sampleDao;
@Inject
public SampleServiceImpl( SampleDao sampleDao)
{
this.sampleDao= sampleDao;
}
@Override
@Validated
public Customer getAccount( String customerKey, String name)
{
try {
return sampleDao.getAccount( customerKey, name);
}
catch ( EmptyResultDataAccessException e ) {
throw new NotFoundException( e );
}
}