如何 Spring 数据 REST return hibernate-JPA 验证和服务器内部错误 json 不是堆栈跟踪

How Spring Data REST return hibernate-JPA validation & server internal errors in json not stack trace

我想在 JSON 格式上获取 JPA 验证和服务器内部错误,而不是堆栈跟踪,

使用Spring JPA data rest

HTTP Status 500 - Request processing failed; nested exception is org.springframework.data.rest.core.RepositoryConstraintViolationException:Validation failed org.springframework.data.rest.core.event.ValidatingRepositoryEventListener.validate(ValidatingRepositoryEventListener.java:179) org.springframework.data.rest.core.event.ValidatingRepositoryEventListener.onBeforeCreate(ValidatingRepositoryEventListener.java:96) org.springframework.data.rest.core.event.AbstractRepositoryEventListener.onApplicationEvent(AbstractRepositoryEventListener.java:50) org.springframework.data.rest.core.event.AbstractRepositoryEventListener.onApplicationEvent(AbstractRepositoryEventListener.java:29) org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383) org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337) org.springframework.data.rest.webmvc.RepositoryEntityController.createAndReturn(RepositoryEntityController.java:484) org.springframework.data.rest.webmvc.RepositoryEntityController.postCollectionResource(RepositoryEntityController.java:272) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:483) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) javax.servlet.http.HttpServlet.service(HttpServlet.java:648) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

将此行添加到 Spring-config.xml

<context:annotation-config/>
<bean class="path.to.config.RestValidationConfiguration" />

@Configuration
public class RestValidationConfiguration extends RepositoryRestConfigurerAdapter {

    @Bean
    @Primary
    /**
     * Create a validator to use in bean validation - primary to be able to
     * Autowire without qualifier
     */
    Validator validator() {
        return new LocalValidatorFactoryBean();
    }

    @Override
    public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
        Validator validator = validator();
        // bean validation always before save and create
        validatingListener.addValidator("beforeCreate", validator);
        validatingListener.addValidator("beforeSave", validator);
    }
}

但是服务器保持returning Stack trace没有JSON错误格式。

任何帮助将不胜感激,谢谢。

感谢 @Alan Hay,提出这个 Answer 与我一起工作。