如何从REST端点捕获JsonParseException [英] How to catch JsonParseException from REST endpoint

查看:333
本文介绍了如何从REST端点捕获JsonParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的端点:

  @POST 
公共响应更新(MyDocument myDocument){}

如果请求无效,我的服务器会得到一些很长的日志:

  javax.servlet.ServletException:org.glassfish.jersey.server.ContainerException:com.fasterxml.jackson.core.JsonParseException:意外字符.. .. 
...
由......
...
引起......

异常很难完全避免,所以我想知道如何捕获JsonParseException?

解决方案

实施 <$ href =https://fasterxml.github.io/jackson-core/javadoc/2.9/com/fasterxml/jackson/core/ ExceptionMapper JsonParseException.htmlrel =nofollow noreferrer> JsonParseException 。它允许您将给定的异常映射到响应。请参见下面的示例:



<预类= 郎-java的prettyprint-越权> @Provider
公共类JsonParseExceptionMapper实现ExceptionMapper< JsonParseException> {

@Override
public Response toResponse(JsonParseException exception){
return Response.status(Response.Status.BAD_REQUEST)
.entity(无法解析JSON )
.type(MediaType.TEXT_PLAIN)
.build();
}
}

然后使用绑定优先级注册它 ResourceConfig 子类(见注释)

  @ApplicationPath(api)
public class JerseyConfig extends ResourceConfig {

public JerseyConfig(){
register(JsonParseExceptionMapper.class,1) ;
}
}

如果你没有使用 ResourceConfig 子类,则可以注释 ExceptionMapper @Priority (见注释)

  @Provider 
@Priority(1)
公共类JsonParseExceptionMapper实现ExceptionMapper< JsonParseException> {
...
}






注1:您可能还会发现创建另一个 ExceptionMapper JsonMappingException



注2:优先考虑您自己的 ExceptionMapper s如果你有< a href =https://jersey.github.io/apidocs/latest/jersey/org/glassfish/jersey/jackson/JacksonFeature.html\"rel =nofollow noreferrer> JacksonFeature ,您希望覆盖 JsonParseExceptionMapper JsonMappingExceptionMapper 附带 jackson-jaxrs-json-provider 模块。有关详细信息,请参阅此答案


I have an endpoint like this:

@POST
public Response update(MyDocument myDocument){}

If the request is not valid, my server would get some quite long logs like this:

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: com.fasterxml.jackson.core.JsonParseException: Unexpected character....
...
Caused by...
...
Caused by...

The exception is hard to be avoided completely, so I am wondering how could I catch the JsonParseException?

解决方案

Implement an ExceptionMapper for JsonParseException. It will allow you to map the given exception to a response. See the example below:

@Provider
public class JsonParseExceptionMapper implements ExceptionMapper<JsonParseException> {

    @Override
    public Response toResponse(JsonParseException exception) {
        return Response.status(Response.Status.BAD_REQUEST)
                       .entity("Cannot parse JSON")
                       .type(MediaType.TEXT_PLAIN)
                       .build();
    }
}

And then register it with a binding priority in your ResourceConfig subclass (see notes):

@ApplicationPath("api")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(JsonParseExceptionMapper.class, 1);
    }
}

If you are not using a ResourceConfig subclass, you can annotate the ExceptionMapper with @Priority (see notes):

@Provider
@Priority(1)
public class JsonParseExceptionMapper implements ExceptionMapper<JsonParseException> {
    ...
}


Note 1: You may also find it helpful to create another ExceptionMapper for JsonMappingException.

Note 2: Giving a priority to your own ExceptionMappers is particularly useful if you have the JacksonFeature registered and you want to override the behavior of JsonParseExceptionMapper and JsonMappingExceptionMapper that come with the jackson-jaxrs-json-provider module. See this answer for further details.

这篇关于如何从REST端点捕获JsonParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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