简体   繁体   中英

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. 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. And the body is not possivel to get with request.getReader() .

There is probably no official API to get the input parameters again, it would mean reading again the request 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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