本文介绍了在spring-data-rest中使用验证器返回http 500而不是400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get the validation in spring-data-rest to work. From the documentation you only need to make a validator available, and I've got that to work, but when a validation constraint is successfully caught/processed I get a 500 error page with the stack trace.

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

In the config class, RepositoryRestMvcConfiguration it has a validationExceptionHandler which looks like it should get such validation errors to return as 400 rather than 500. It is also a lazy loaded bean.

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

Do I have an incorrect setup? or is there another way to get spring-data-rest to return 400 instead of 500?

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

I'm using spring-data-rest version 2.0.0 Release

tomcat的堆栈跟踪返回:

Stack trace return by 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:

RestMvcConfig:

@Configuration
public class RestExporterRestConfig extends RepositoryRestMvcConfiguration {}

推荐答案

以前的答案对我不起作用,我认为由于Spring Data Rest的变化,所以这里有一个更新的答案,它确实适用于JPA和MongoDb,节省其他人为此花费很多时间.

The previous answers didn't work for me, I think due to changes in Spring Data Rest so here is an updated answer that did work with JPA and MongoDb to save anyone else spending ages on this.

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

Had to add this to my build.gradle dependencies

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

这篇关于在spring-data-rest中使用验证器返回http 500而不是400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:43