如何将 JSON 解析相关错误映射到球衣中的 json 响应

BAE*_*BAE 6 java rest error-handling jersey-2.0 exceptionmapper

我定义了一个数据类型及其 API:

public class DataType {
    @Column
    private String name;
}

// API is:
public class DataTypeAPI {
    @POST
    @Path("/")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ManagedAsync
    public void createDataType(
            final DataType dataType,
    ) {
        ...
        asyncResponse.resume(Response.status(Response.Status.CREATED)
                .entity(dataType)
                .build());
    }
}
Run Code Online (Sandbox Code Playgroud)

{ "name": "xxx" }只要我摆好姿势一切都好

但当我发帖时{ "name1": "xxx" },我得到了以下text/plain回复:

Unrecognized field "name1" (class com.xxx.datatypes.DataType), not marked as ignorable (1 known properties: "name"])
 at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@526e34b1; line: 2, column: 15] (through reference chain: com.xxx.datatypes.DataType["name1"])
Run Code Online (Sandbox Code Playgroud)

我更喜欢将上述错误转换为 JSON 响应。但事件我添加了以下异常映射器,它没有返回 JSON 响应。

@Provider

public class GenericExceptionMapper implements ExceptionMapper<Throwable> {
    public Response toResponse(Throwable ex) {
        System.out.println("GenericExceptionMapper");
        ex.printStackTrace();
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                .entity(new ErrorBody(500, ex.getMessage()))
                .type(MediaType.APPLICATION_JSON)
                .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我的上述异常映射器无法捕获球衣解析错误。有人可以向我解释一下吗?谢谢

更新

我有两个问题:

1、如何让响应变成beapplication/json而不是text/plain

2、为什么我的异常映射器没有捕获引发的异常并将其映射到 json 响应?

更新

我添加了以下映射器:

public class JsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException> {
    public Response toResponse(JsonMappingException ex) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                .entity(new ErrorBody(500, ex.getMessage()))
                .type(MediaType.APPLICATION_JSON)
                .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我可以获得 json 响应: { "code": 500, "message": "Unrecognized field \"name1\" (class com.xxx.datatypes.DataType), not marked as ignorable (1 known properties: \"name\"])\n at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@1d8cd12a; line: 2, column: 15] (through reference chain: class com.xxx.datatypes.DataType[\"name1\"])" }

我还有两个问题:

1,为什么我的通用异常映射器无法捕获它。

2、 json映射异常映射器在发布时无法映射以下异常(逗号,添加用于测试)Unexpected character ('}' (code 125)): was expecting double-quote to start field name at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@7615743c; line: 3, column: 2]{"name": "xxxx",}

Rav*_*avi 0

无法识别的字段“name1”(com.xxx.datatypes.DataType 类),未标记为可忽略(1 个已知属性:“name”])

如果您查看引发的异常,很明显,您的 POJO 类具有名称为namenot的属性name1

并且,您正在尝试使用属性 name 发布 JSON name1

{ "name1": "xxx" }
Run Code Online (Sandbox Code Playgroud)

而且,它没有被识别,因为name1没有定义name

public class DataType {
    @Column
    private String name;
}
Run Code Online (Sandbox Code Playgroud)

这就是原因,在其他情况下,当您将 JSON 发布为

{ "name": "xxx" }
Run Code Online (Sandbox Code Playgroud)