我一直在尝试将 spring 验证器添加到 spring-data-rest 项目中。
我跟着并通过此链接设置“入门”应用程序:http: //spring.io/guides/gs/accessing-data-rest/
...现在我正在尝试按照此处的文档添加自定义 PeopleValidator:http: //docs.spring.io/spring-data/rest/docs/2.1.0.RELEASE/reference/html/validation-chapter .html
我的自定义 PeopleValidator 看起来像
package hello;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
public class PeopleValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return true;
}
@Override
public void validate(Object target, Errors errors) {
errors.reject("DIE");
}
}
...我的 Application.java 类现在看起来像这样
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public PeopleValidator beforeCreatePeopleValidator() {
return new PeopleValidator();
}
}
我希望发布到http://localhost:8080/people
URL 会导致某种错误,因为 PeopleValidator 拒绝所有内容。但是,不会引发错误,并且永远不会调用验证器。
我还尝试手动设置验证器,如 spring-data-rest 文档的第 5.1 节所示。
我错过了什么?