简体   繁体   English

如何在Jersey的ExceptionMapper上获取请求的信息

[英]How to get informations of request on ExceptionMapper of Jersey

when i throw an Throwable i want save the information of request on my hard drive for debug. 当我抛出Throwable时,我想将请求的信息保存在硬盘上以进行调试。 Its possible? 这是可能的?

ex: 例如:

@Provider
public class DefaultExceptionMapper implements ExceptionMapper<Throwable> {

    @Override
    public Response toResponse(Throwable exception) {
        exception.printStackTrace();

        JSONObject msg = new JSONObject();
        msg.put("msg", "Try again.");

        // i need something like this

        JSONObject request = SuperEspecialClass.getActualRequest().parseToJSON();
        request.saveOnHardDriveForDebugLater();

        /**request contains -> all headers
        request contains -> all parameters
        request contains -> body 
        request contains -> method
        request contains -> URI

        like this :

        {
            url : '/url',
            method : '',
            headers : [],
            params : [],

            body : ''
        }*/

        return Response
                .serverError()
                .entity(msg)
                .build();
    }

}

with @Context HttpServletRequest the form parameters is gone. 使用@Context HttpServletRequest的表单参数不见了。 And the body is not possivel to get with request.getReader() . 而且主体不是通过request.getReader()

There is probably no official API to get the input parameters again, it would mean reading again the request InputStream. 可能没有官方API可以再次获取输入参数,这将意味着再次读取请求InputStream。

You could implement an Interceptor which saves the method parameters before proceeding with the request and retrieve them in case of exception. 您可以实现一个拦截器 ,该拦截器在进行请求之前保存方法参数,并在出现异常的情况下进行检索。 You have to pay extra attention to thread safety of the interceptor: you don't want to mix exceptions and parameters from different requests. 您必须特别注意拦截器的线程安全性:您不想混合来自不同请求的异常和参数。 Also note that the exception could come during deserialization, validation or matching of the request, in that case you would not have parameters at all. 另请注意,在反序列化,验证或请求匹配期间可能会发生异常,在这种情况下,您根本没有参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM