无效的Json字符串的Jersey自定义异常映射器 [英] Jersey Custom Exception Mapper for Invalid Json String

查看:485
本文介绍了无效的Json字符串的Jersey自定义异常映射器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,如果请求中发送了无效的JSON有效负载,我需要为其构建响应.

I am working on an application for which I required to build the response in case an invalid JSON payload is sent in the request.

当前在服务器端收到的异常消息为意外字符("(代码34)):期望逗号分隔对象条目 在[资料来源:org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@310db662;行:13,列:7]

Currently the exception message received at the server end is Unexpected character ('"' (code 34)): was expecting comma to separate OBJECT entries at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@310db662; line: 13, column: 7]

这就是我试图实现的目标...

This is how I'm trying to achieve it ...

import java.util.ArrayList;
import java.util.List;

import javax.validation.ConstraintDeclarationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Provider
public class JsonParseExceptionMapper implements ExceptionMapper<JsonProcessingException> {

    private static Logger logger = LogManager.getLogger(JsonParseExceptionMapper.class.getName());

    @Override
    public Response toResponse(final JsonProcessingException ex) {

        System.out.println("inside json parse exception mappper");

        logger.error(messages.toString());
        return Response.status(Response.Status.BAD_REQUEST).entity(messages).type(MediaType.APPLICATION_JSON).build();
    }
}

我也有一个类似的ExceptionMapper,它捕获了工作正常的ConstraintViolationException.

I also have a similar ExceptionMapper which catches the ConstraintViolationException which is working fine.

我还尝试了互联网上建议的一些不同的例外情况,仅举几例

I also tried a few different exceptions as suggested over the internet, to name a few

JsonProcessingException
BadRequestException
JsonParseException
InvalidFormatException
ConstraintDeclarationException

但是在这种情况下,以上所有异常似乎均无效.有谁知道应将哪个异常与ExceptionMapper一起使用来捕获此类情况.
另外,我猜可能有多个平行运行的ExceptionMapper实现.如果我错了,请更正.

But None of the above exceptions seem to work in this case. Does anyone have any idea as to which exception should be used in conjunction with the ExceptionMapper to catch such cases.
Also, I guess it is possible to have more than one ExceptionMapper implementations running parallel to each other. Please correct if I'm wrong.

这是我的意思,这是无效的JSON有效负载,请在校验和之前查找缺失的逗号

{
    "payload": {
        "name":"rachel",
        "employeeid":"23443"
    }
    "checksum":""
}

Jersey v2.22.1
Spring v4.2.4-RELEASE

Jersey v2.22.1
Spring v4.2.4-RELEASE

由于某些原因,我无法对任何帖子发表评论!!!即使编辑窗口出现问题,我也看不到任何图标!!

我也尝试使用RuntimeException,Exception和IOException,但是ExceptionMapper也不适用于它们.我猜这需要一个特定的异常时间,例如ConstraintViolationException,该异常正在与另一个ExceptionMapper一起使用,并且工作正常.

I tried using RuntimeException, Exception and IOException as well but the ExceptionMapper didn't work for them too. I guess it takes a specific time of exception like the ConstraintViolationException which is in use with another ExceptionMapper and that one is working fine.

web.xml的某些部分 jersey.config.beanValidation.enableOutputValidationErrorEntity.server 真的 jersey.config.server.disableMoxyJson 真的

some part of web.xml jersey.config.beanValidation.enableOutputValidationErrorEntity.server true jersey.config.server.disableMoxyJson true

推荐答案

最初,我为Json Parsing注册了JacksonFeature.class,该类使用自己的ExceptionMapper进行JsonParseException.

Initially I had registered JacksonFeature.class for Json Parsing which uses its own ExceptionMapper for JsonParseException.

我将其更改为JacksonJaxbJsonProvider.class.

I changed it to JacksonJaxbJsonProvider.class.

我现在可以使用自定义的JsonParseExceptionMapper.

I'm now able to use my custom JsonParseExceptionMapper.

必须更改某些依赖项!

这篇关于无效的Json字符串的Jersey自定义异常映射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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