Spring-Data-Rest验证器 [英] Spring-Data-Rest Validator

查看:150
本文介绍了Spring-Data-Rest验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将spring验证器添加到spring-data-rest项目中。

I have been trying to add spring validators to a spring-data-rest project.

我跟着并通过以下链接设置入门应用程序: http://spring.io/guides/gs/accessing-data-rest/

I followed along and setup the "getting started" application via this link: 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

...and now I am trying to add a custom PeopleValidator by following the documents here: http://docs.spring.io/spring-data/rest/docs/2.1.0.RELEASE/reference/html/validation-chapter.html

我的自定义PeopleValidator看起来像

My custom PeopleValidator looks like

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类现在看起来像这样

...and my Application.java class now looks like this

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();
    }
}

我希望POST到 http:// localhost:8080 / people 由于PeopleValidator拒绝所有内容,因此URL会导致某种错误。但是,不会抛出任何错误,并且永远不会调用验证器。

I would expect that POSTing to the http://localhost:8080/people URL would result in an error of some kind since the PeopleValidator is rejecting everything. However, no error is thrown, and the validator is never called.

我还尝试手动设置验证器,如spring-data-rest的5.1节所示文件。

I have also tried manually setting up the validator as shown in section 5.1 of the spring-data-rest documentation.

我缺少什么?

推荐答案

所以看来前/后保存事件仅在PUT和PATCH上触发。当POSTing时,前面/后面的创建事件触发。

So it appears that the before/after "save" events only fire on PUT and PATCH. When POSTing, the before/after "create" events fire.

我使用 configureValidatingRepositoryEventListener 覆盖并且有效。我不确定我在工作中做的与在家做的不同。我明天要看。

I tried it the manual way again using the configureValidatingRepositoryEventListener override and it worked. I'm not sure what I'm doing differently at work than here at home. I'll have to look tomorrow.

我很乐意听到其他人是否有人建议为什么它不起作用。

I sure would love to hear if others have suggestions on why it wouldn't work.

记录,这是新的Application.java类的样子。

For the record, here is what the new Application.java class looks like.

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.core.event.ValidatingRepositoryEventListener;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;

@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application extends RepositoryRestMvcConfiguration {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
        validatingListener.addValidator("beforeCreate", new PeopleValidator());
    }
}

这篇关于Spring-Data-Rest验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆