泽西岛自定义ExceptionMapper未在400 Bad Request上调用(验证失败时)

时间:2016-01-28 15:07:08

标签: spring jersey-2.0

我使用Jersey和Spring进行网络服务。为了捕获异常并格式化发送给调用者的响应,我添加了ExceptionMapper的实现。

虽然在我从控制器中显式抛出异常时调用它,但是当json字段验证失败时,不调用异常映射器并且发送的响应**可能不为null(path = checkNotification.arg0。轮胎,invalidValue = null) **

@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

    @Override
    public Response toResponse(Throwable ex) {
        System.out.println("Exception Mapper !!!");
        return Response.status(404).entity(ex.getMessage()).type("application/json").build();
    }
}


<servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
            <init-param>
                <param-name>jersey.config.server.provider.packages</param-name>
                <param-value>
                        com.help.rest.controller,
                        com.fasterxml.jackson.jaxrs.json
                </param-value>
            </init-param>
            <init-param>
                    <param-name>javax.ws.rs.Application</param-name>
                    <param-value>com.help.filter.FeatureRegistration</param-value>
            </init-param>
            <init-param>
                <param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
                <param-value>true</param-value>
            </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

泽西岛版本是2.22.1 Spring Version是4.2.4

我通过改变

使其成功
 @Provider
    public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

        @Override
        public Response toResponse(Throwable ex) {
            System.out.println("Exception Mapper !!!");
            return Response.status(404).entity(ex.getMessage()).type("application/json").build();
        }
    }

 @Provider
    public class GenericExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

        @Override
        public Response toResponse(ConstraintViolationException ex) {
            System.out.println("Exception Mapper !!!");
            return Response.status(404).entity(ex.getMessage()).type("application/json").build();
        }
    }

虽然我能够捕获异常,但我没有得到确实没有约束的类和字段。

解决 找到一个包含ContraintViolationException中所有必填字段的Set,可以使用ex.getConstraintViolations()

访问

2 个答案:

答案 0 :(得分:3)

通过更改

使其成功
 @Provider
    public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

        @Override
        public Response toResponse(Throwable ex) {
            System.out.println("Exception Mapper !!!");
            return Response.status(404).entity(ex.getMessage()).type("application/json").build();
        }
    }

 @Provider
    public class GenericExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

        @Override
        public Response toResponse(ConstraintViolationException ex) {
            System.out.println("Exception Mapper !!!");
            return Response.status(404).entity(ex.getMessage()).type("application/json").build();
        }
    }

要获取确切的异常详细信息,例如哪个字段失败,则使用ConstraintViolationException的getConstraintViolations()。此方法为所有约束违规提供了一个集合。

答案 1 :(得分:0)

您需要注册:

public class MyApplication extends Application {
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(HelloWorldResource.class);

        /** you need to add ExceptionMapper class as well **/
        s.add(GenericExceptionMapper.class)
        return s;
    }
}