在spring-data-rest中使用验证器返回http 500而不是400

时间:2014-02-26 01:17:21

标签: spring spring-mvc spring-data-rest

我正在尝试在spring-data-rest中进行验证。从文档中你只需要提供一个验证器,我就可以使用它,但是当成功捕获/处理验证约束时,我会得到一个带有堆栈跟踪的500错误页面。

在config类中,RepositoryRestMvcConfiguration它有一个validationExceptionHandler,它看起来应该得到这样的验证错误,返回400而不是500.它也是一个延迟加载的bean。

我的设置不正确吗?还是有另一种方法让spring-data-rest返回400而不是500?

我正在使用spring-data-rest版本2.0.0版本

通过tomcat返回堆栈跟踪:

HTTP Status 500 - Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [test.domain.Account] during persist time for groups [javax.validation.groups.Default, ]

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [test.domain.Account] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='size must be between 0 and 10', propertyPath=login, rootBeanClass=class test.domain.Account, messageTemplate='{javax.validation.constraints.Size.message}'}
]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

帐户实体:

@Entity
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    @Column(unique = true)
    @Size(max = 10)
    String login;

}

RestMvcConfig:

@Configuration
public class RestExporterRestConfig extends RepositoryRestMvcConfiguration {}

2 个答案:

答案 0 :(得分:4)

似乎让它运转起来;我必须覆盖validatingRepositoryEventListener()并手动将验证器添加到监听器;

@Configuration
public class RestExporterRestConfig extends RepositoryRestMvcConfiguration {

    @Bean
    public Validator validator() {
        return new LocalValidatorFactoryBean();
    }

    @Bean
    @Override
    public ValidatingRepositoryEventListener validatingRepositoryEventListener() {
        ValidatingRepositoryEventListener listener = new ValidatingRepositoryEventListener();
        configureValidatingRepositoryEventListener(listener);
        listener.addValidator("afterCreate", validator());
        listener.addValidator("beforeCreate", validator());
        return listener;
    }

}

我现在按照以下方式返回400;

400 Bad Request
{"errors":
    [{  "entity":"Account",
        "message":"size must be between 0 and 10",
        "invalidValue":"login 0dsfdsfdsfdsfdsfdsfdsfds",
        "property":"login"
    }]
}

答案 1 :(得分:4)

以前的答案对我不起作用,我认为由于Spring Data Rest的变化,所以这里有一个更新的答案,它与JPA和MongoDb一起工作,以节省其他人在这方面的花费时间。

必须将此添加到我的build.gradle依赖项

    compile('org.hibernate:hibernate-validator:4.2.0.Final')

和这个配置类

@Configuration
public class CustomRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter {


   @Bean
   public Validator validator() {
       return new LocalValidatorFactoryBean();
   }

   @Override
   public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
       validatingListener.addValidator("afterCreate", validator());
       validatingListener.addValidator("beforeCreate", validator());
       validatingListener.addValidator("afterSave", validator());
       validatingListener.addValidator("beforeSave", validator());
   }
}