Jersey的自定义ExceptionMapper不适用于无效的JSON输入 [英] Custom ExceptionMapper for Jersey not working for invalid JSON input

查看:1261
本文介绍了Jersey的自定义ExceptionMapper不适用于无效的JSON输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下资源消耗了映射到POJO的JSON。

  @Path(example)
public class ExampleResource {

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response addThesis(MyObject myObject){
return Response.ok() 。.entity( 测试)构建();
}
}

这是POJO类:

  public class MyObject {
private String title;

public String getTitle(){
return title;
}

public void setTitle(String title){
this.title = title;
}
}

当我发送身体<强的POST请求时> {title:测试标题} 一切正常。正如预期的那样,响应是测试。但是,当我将请求更改为 {titlee:测试标题} 时,服务器会回复:


无法识别的字段titlee(类com.my.package.MyObject),未标记为可忽略(一个已知属性:title))
at [来源:org.glassfish.jersey.message.internal .ReaderInterceptorExecutor $ @ UnCloseableInputStream 6dc6a46a; line:2,column:11](通过参考链:com.my.package.MyObject [titlee])


显然这是泽西投掷和退回的例外。如何拦截此异常并返回自定义状态代码和消息?



到目前为止我尝试的是实现自己的ExceptionMapper:

  @Provider 
公共类MyJsonExceptionMapper实现ExceptionMapper< JsonProcessingException> {
public Response toResponse(JsonProcessingException e){
return Response.status(400).entity(JSON Processing Error)。build();
}
}

不幸的是,响应保持不变。当我为自定义异常实现ExceptionMapper并在资源方法中抛出相应的异常时,一切正常。我假设这与默认的ExceptionMapper for JsonProcessingException重写我自己的。然后我尝试创建一个通用映射器(实现ExceptionMapper),但是再次没有成功。



我看起来到处都是,并尝试了很多东西,包括扩展ResourceConfig和注册我的映射器,但到目前为止还没有任何工作。



可能有助于缩小问题范围的更多信息:我使用Grizzly2作为我部署的HTTP服务器一个胖JAR。



我的pom.xml的依赖部分如下所示:

 <依赖性> 
< dependency>
< groupId> org.glassfish.jersey.media< / groupId>
< artifactId> jersey-media-json-jackson< / artifactId>
< version> 2.24< / version>
< / dependency>
< dependency>
< groupId> org.glassfish.jersey.containers< / groupId>
< artifactId> jersey-container-grizzly2-http< / artifactId>
< version> 2.24< / version>
< / dependency>
< / dependencies>

非常感谢任何建议。

解决方案

好吧,这是愚蠢和黑客,但对我有用:



register(JacksonJaxbJsonProvider。 class);



这是由于Jackson功能入口点中的以下不错的默认行为:



if(!config.isRegistered(JacksonJaxbJsonProvider.class)){
//添加默认的Jackson异常映射器
context.register(JsonParseExceptionMapper.class) );
context.register(JsonMappingExceptionMapper.class);



:(



<但是,我仍然更喜欢能够解决真实问题的答案 - 即没有预先注册组件,因此该功能无法正确配置它们......


I have the following resource that consumes a JSON being mapped to a POJO.

@Path("example")
public class ExampleResource {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response addThesis(MyObject myObject) {
        return Response.ok().entity("Test").build();
    }
}

Here's the POJO class:

public class MyObject {
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

When I send a POST request with the body {"title":"Test title"} everything works fine. The response is Test, as expected. However, when I change the request to {"titlee":"Test title"} the server replies with this:

Unrecognized field "titlee" (class com.my.package.MyObject), not marked as ignorable (one known property: "title"]) at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@6dc6a46a; line: 2, column: 11] (through reference chain: com.my.package.MyObject["titlee"])

Obviously this is an exception thrown and returned by Jersey. How can I intercept this exception and return a custom status code and message?

What I've tried so far is to implement my own ExceptionMapper:

@Provider
public class MyJsonExceptionMapper implements ExceptionMapper<JsonProcessingException> {
    public Response toResponse(JsonProcessingException e) {
        return Response.status(400).entity("JSON Processing Error").build();
    }
}

Unfortunately the response stays the same. When I implement an ExceptionMapper for a custom exception and throw the corresponding exception in the resource method though, everything works fine. I assume this has to do with the default ExceptionMapper for JsonProcessingException overriding my own one. Then I tried to create a generic mapper ("implements ExceptionMapper"), but again no success.

I've looked literally everywhere and tried many things including extending ResourceConfig and registering my mapper, but nothing has worked so far.

Some more information that might help to narrow the problem down: I am using Grizzly2 as the HTTP server which I am deploying as a Fat JAR.

The dependency part of my pom.xml looks like this:

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.24</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
        <version>2.24</version>
    </dependency>
</dependencies>

Any advice is highly appreciated.

解决方案

Ok, this is dumb and hack-ish, but worked for me:

register(JacksonJaxbJsonProvider.class);

This is due to the following "nice default behavior" in the Jackson feature entry point:

if (!config.isRegistered(JacksonJaxbJsonProvider.class)) { // add the default Jackson exception mappers context.register(JsonParseExceptionMapper.class); context.register(JsonMappingExceptionMapper.class);

:(

But, I'd still prefer an answer that fixes the problem "for real" - ie. without pre-registering components so that the feature cannot configure them properly...

这篇关于Jersey的自定义ExceptionMapper不适用于无效的JSON输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆