当杰克逊反序列化失败时,Jersey异常映射器无法正常工作 [英] Jersey Exception mappers not working when jackson deserialization fails

查看:165
本文介绍了当杰克逊反序列化失败时,Jersey异常映射器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在REST API中使用Jersey 2.10和Jackson序列化/反序列化功能。

I am using Jersey 2.10 with Jackson serialization/deserialization feature in my REST API.

我的想法是让我的REST API始终返回标准的JSON错误响应。为此,我有ExceptionMapper类,为Jersey应用程序中抛出的任何异常构建正确的json错误响应。我还有一个生成相同类型的JSON响应的jsp,我在web.xml中注册为error-page,它涵盖了Jersey加载之前可能出现的所有错误。

My idea is to make my REST API to always return a standard JSON error response. For that I have ExceptionMapper classes that build proper json error responses for any exception being thrown in the Jersey application. I also have a jsp which produces the same kind of JSON response, which I registered as error-page in the web.xml that covers all the errors that could come before Jersey being loaded.

但是有一种情况,我的Exception mappers和我的json生成jsp都没有工作,也就是将一个错误形成的json发送到POST REST端点,它只返回以下消息:

But there is one case in which neither my Exception mappers nor my json producing jsp are working, that is when sending a bad formed json to a POST REST endpoint which just returns the following message:

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Content-Type: text/plain
Content-Length: 210
Date: Tue, 24 Jun 2014 22:14:11 GMT
Connection: close

Can not deserialize instance of com.example.rest.User[] out of START_OBJECT token
 at [Source: org.glassfish.jersey.message.internal.EntityInputStream@1dcccac; line: 1, column: 1]

如何让Jersey返回我的自定义错误响应而不是这个?

How can I make Jersey to return my custom error response instead of this?

更新:

根据@Lucasz的回答,我做了更多的研究,发现在com.fasterxml.jackson.jaxrs.base包中定义了两个异常映射器( https://github.com/FasterXML/jackson-jaxrs-providers/tree/master/base/src/main/ java / com / fasterxml / jackson / jaxrs / base )JsonMappingExceptionMapper和JsonParseExceptionMapper似乎是在影响我的自定义映射器。

Based on the answer by @Lucasz, I did more research and found that there are two Exception mappers defined inside the package com.fasterxml.jackson.jaxrs.base (https://github.com/FasterXML/jackson-jaxrs-providers/tree/master/base/src/main/java/com/fasterxml/jackson/jaxrs/base) JsonMappingExceptionMapper and JsonParseExceptionMapper that seem to be shadowing my custom mappers.

如何取消注册这些映射器?

How can I unregister those mappers?

这就是我目前正在注册地图制作者的方式:

This is how I am currently registering the mappers:

@ApplicationPath("/")
public class MyApp extends ResourceConfig{
    public SyntheticAPIApp() {
        packages("com.example.resource", "com.example.mapper");
        register(org.glassfish.jersey.jackson.JacksonFeature.class);
    }
}


推荐答案

我使用如下的异常映射器测试它:

I tested it with an exception mapper like below:

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.core.JsonProcessingException;

@Provider
public class JsonProcessingExceptionMapper implements ExceptionMapper<JsonProcessingException>{

        public static class Error {
            public String key;
            public String message;
        }

        @Override
        public Response toResponse(JsonProcessingException exception) {
            Error error = new Error();
            error.key = "bad-json";
            error.message = exception.getMessage();
            return Response.status(Status.BAD_REQUEST).entity(error).build();
        }
}

并且有效。

更新:将JsonParseException更改为JsonProcessingException(更一般)

Update: changed JsonParseException to JsonProcessingException (more general)

Update2:
为了避免注册不需要的映射器,请替换

Update2: In order to avoid registering the unwanted mappers replace

register(org.glassfish.jersey.jackson.JacksonFeature.class);

with

register(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider.class);

查看 JacksonFeature ,你会明白发生了什么。

Look at the source code of JacksonFeature and you'll understand what's happening.

这篇关于当杰克逊反序列化失败时,Jersey异常映射器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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