当杰克逊反序列化失败时,泽西异常映射器不起作用

Jersey Exception mappers not working when jackson deserialization fails(当杰克逊反序列化失败时,泽西异常映射器不起作用)

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

问题描述

我在我的 REST API 中使用带有 Jackson 序列化/反序列化功能的 Jersey 2.10.

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

我的想法是让我的 REST API 始终返回标准 JSON 错误响应.为此,我有 ExceptionMapper 类,可以为 Jersey 应用程序中抛出的任何异常构建正确的 json 错误响应.我还有一个 jsp,它产生相同类型的 JSON 响应,我在 web.xml 中将其注册为错误页面,涵盖了加载 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.

但是在一种情况下,我的异常映射器和生成 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)

更新 2:为了避免注册不需要的映射器替换

Update2: In order to avoid registering the unwanted mappers replace

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

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

查看JacksonFeature 你就会明白发生了什么.

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

这篇关于当杰克逊反序列化失败时,泽西异常映射器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:当杰克逊反序列化失败时,泽西异常映射器不起作用

基础教程推荐