How to log request body in JAX-RS client

前端 未结 1 719
遥遥无期
遥遥无期 2020-12-30 00:30

I need to see my request body with a client JAX-RS request in order to verify that the serialization works correct and reuse the request for test clients like Postman.

相关标签:
1条回答
  • 2020-12-30 01:05

    JAX-RS 2.0 (which it looks like you're using), has the ClientRequestFilter. You can register it with the Client or even the WebTarget. From the filter method, you can get the entity, and do your logging

    public class LoggingFilter implements ClientRequestFilter {
        private static final Logger LOG = Logger.getLogger(LoggingFilter.class.getName());
    
        @Override
        public void filter(ClientRequestContext requestContext) throws IOException {
            LOG.log(Level.INFO, requestContext.getEntity().toString());
        }
    }
    
    [...]
    
    Client client = ClientBuilder.newClient();
    client.register(new LoggingFilter());
    

    Also the ClientRequestContext API for some other goodies you might find interesting.

    UPDATE

    See Also:

    • JAX-RS 2 print JSON request, for a complete/better implementation.
    0 讨论(0)
提交回复
热议问题