Jersey - 在无效的POST请求中未调用自定义异常映射器

时间:2016-06-20 08:21:41

标签: java jersey jax-rs jersey-2.0

我正在使用Jersey 2.22.1来实现REST API。

我创建了一个自定义异常映射器,将RuntimeException映射到我选择的特定HTTP响应代码。

@Provider
public class MyExceptionMapper implements ExceptionMapper<RuntimeException>
{
    @Override
    public Response toResponse(RuntimeException exception)
    {
        ....
    }
}

我的资源类被配置为为POST请求获取许多参数。

@Path("rest/api/1.0/test")
public class MyResource
{
    @NotNull(message = "Missing parameter 'param1'")
    @FormParam("param1")
    private String m_param1;

    @NotNull(message = "Missing parameter 'param2'")
    @FormParam("param2")
    private String m_param2;

    @POST
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes("application/x-www-form-urlencoded")
    public Response test(String body)
    {
        ...
    }
}

当我发送没有任何参数的POST请求时:

curl -X POST http://192.168.0.2:9989/myApp/rest/api/1.0/test

我期待调用自定义异常映射器,但事实并非如此。相反,我得到了一堆IllegalStateException

WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 6
java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded
    at org.glassfish.jersey.server.internal.inject.FormParamValueFactoryProvider$FormParamValueFactory.ensureValidRequest(FormParamValueFactoryProvider.java:183)
    at org.glassfish.jersey.server.internal.inject.FormParamValueFactoryProvider$FormParamValueFactory.getForm(FormParamValueFactoryProvider.java:167)
    at org.glassfish.jersey.server.internal.inject.FormParamValueFactoryProvider$FormParamValueFactory.provide(FormParamValueFactoryProvider.java:118)
    at org.glassfish.jersey.server.internal.inject.ParamInjectionResolver.resolve(ParamInjectionResolver.java:134)
    at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:211)
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:234)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2072)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:767)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:706)
    at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)
    at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:284)
    at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
    at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:92)
    at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61)
    at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:318)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:384)
    at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:224)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)

有人可以解释为什么我的自定义异常映射器没有被调用吗?

请注意,如果我在我的资源方法中抛出RuntimeException然后发送有效的POST请求,则会调用异常映射器。所以我知道在某种程度上是否有效。

更新

我发现如果我将以下内容添加到HTTP请求中:

-H "Content-Type: application/x-www-form-urlencoded"

然后我的ConstraintViolationExceptionMapper被调用。

@Provider
public class ConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException>
{
    ...
}

但如果删除ConstraintViolationExceptionMapper,我的自定义异常映射器仍未被调用。

2 个答案:

答案 0 :(得分:1)

更新前错误

遵循JAX-RS规范,对于@XxxParam和其他ConstraintViolationException,当解析param期间发生异常时,Jersey会抛出已映射的异常。这是为了遵循规范以返回正确的响应状态。所以我们从来没有机会处理它。

更新问题

泽西岛已经有mapper for ValidationException,这是ConstrainViolationException的超类。因此,当您为ValidationException提供映射器时,它比ValidationException更具体,因此会被调用。

  

即使我为{{1}}提供了一个映射器,当我发送没有标头的请求时也不会调用它。你知道为什么吗?

因为param解析发生在验证之前。

答案 1 :(得分:0)

被接受的答案似乎不正确(不再)。

  

所以我们永远没有机会来处理它。

现在不是真的。在https://stackoverflow.com/a/45478604/1271372中检查 什么是正确的解决方案

我紧随其后,实现了ExceptionMapper<SomeParseException>并将其注册到JacksonFeature之前的资源配置register(ExceptionMapperImplementation.class, 1)中,它的工作原理就像是一种魅力。请注意优先级1。

当然,这应该返回代码400。我必须将响应主体自定义为JSON,而不是纯文本。