Jersey:打印实际请求

发布于 2024-11-26 22:45:38 字数 72 浏览 1 评论 0原文

如何查看 Jersey 生成并发送到服务器的实际请求?我遇到了特定请求的问题,运行网络服务器的人要求查看完整的请求(带有标头等)。

How can I view the actual request that Jersey generates and sends to the server? I am having issues with a particular request and the fellow running the webserver asked to see the full request (with headers and the such).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沙与沫 2024-12-03 22:45:38

如果您只是使用 Jersey Client API,LoggingFilter (客户端过滤器)应该可以帮助您:

Client client = Client.create();
client.addFilter(new LoggingFilter(System.out));
WebResource webResource = client.resource("http://localhost:9998/");
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                                         .get(ClientResponse.class);

否则,您可以使用其他 LoggingFilter (容器筛选)。

If you're just using Jersey Client API, LoggingFilter (client filter) should help you:

Client client = Client.create();
client.addFilter(new LoggingFilter(System.out));
WebResource webResource = client.resource("http://localhost:9998/");
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                                         .get(ClientResponse.class);

Otherwise, you can again log both request and response on server using other LoggingFilter (container filter).

命比纸薄 2024-12-03 22:45:38

Jersey 2.23开始,有一个LoggingFeature 您可以使用。以下是一个稍微简化的示例,请注意,您可以在 WebTarget 也是如此。

Logger logger = Logger.getLogger(getClass().getName());

Feature feature = new LoggingFeature(logger, Level.INFO, null, null);

Client client = ClientBuilder.newBuilder()
        .register(feature)
        .build();

Response response = client.target("https://www.google.com")
        .queryParam("q", "Hello, World!")
        .request().get();

LoggingFeature 的 JavaDoc 表示请求“和/或”响应被记录下来,哈哈。在我的机器上,两者都被记录。

Since Jersey 2.23, there's a LoggingFeature you could use. The following is a bit simplified example, please note that you can register the feature on WebTarget as well.

Logger logger = Logger.getLogger(getClass().getName());

Feature feature = new LoggingFeature(logger, Level.INFO, null, null);

Client client = ClientBuilder.newBuilder()
        .register(feature)
        .build();

Response response = client.target("https://www.google.com")
        .queryParam("q", "Hello, World!")
        .request().get();

JavaDoc of LoggingFeature says that the request "and/or" the response is logged lol. On my machine, both are logged.

时光磨忆 2024-12-03 22:45:38

@ivan.cikic 的答案适用于 Jersey 1.x。这是在 Jersey 2.x 中执行此操作的方法:

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.filter.LoggingFilter;
import org.json.JSONException;
import org.json.JSONObject;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;

...

        ClientConfig config = new ClientConfig();

        Client client = ClientBuilder.newClient(config);
        client.register(new LoggingFilter());

这无关紧要,但我只是不得不抱怨一下:新的 LoggingFilter 确实很烦人,因为它强制您使用 Java Util Logging 。如果它能让我控制记录器那就更好了。看起来像是设计上的倒退。

@ivan.cikic's answer is for Jersey 1.x. Here's how you do it in Jersey 2.x:

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.filter.LoggingFilter;
import org.json.JSONException;
import org.json.JSONObject;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;

...

        ClientConfig config = new ClientConfig();

        Client client = ClientBuilder.newClient(config);
        client.register(new LoggingFilter());

This is irrelevant but I just have to complain: The new LoggingFilter is really annoying because it forces you to use Java Util Logging. It would be better if it gave me control over the logger. Seems like a step backwards in design.

冷︶言冷语的世界 2024-12-03 22:45:38

所有这些答案都非常接近,但它们缺乏记录请求和响应正文的设置。至少在 Jersey 2.30.1 中,这就是我完成记录请求和响应(包括它们各自的正文)的方式:

import javax.ws.rs.client.ClientBuilder;
import org.glassfish.jersey.logging.LoggingFeature;
import java.util.logging.Level;
import java.util.logging.Logger;

Logger logger = Logger.getLogger("LoggingFeature");
logger.setLevel(Level.ALL);
ClientBuilder.newClient()
  .target("https://www.example.com")
  .register(new LoggingFeature(
    logger,
    Level.ALL,
    LoggingFeature.Verbosity.PAYLOAD_ANY,
    8192))
  .request()
  .get();

从技术上讲,Level.All8192 值可能为 null。我只是在这里提供它们以简洁起见。

All these answers are pretty close but they lack the setting to log the request and response body. At least with Jersey 2.30.1 this is how I accomplish logging the request and response including their respective bodies:

import javax.ws.rs.client.ClientBuilder;
import org.glassfish.jersey.logging.LoggingFeature;
import java.util.logging.Level;
import java.util.logging.Logger;

Logger logger = Logger.getLogger("LoggingFeature");
logger.setLevel(Level.ALL);
ClientBuilder.newClient()
  .target("https://www.example.com")
  .register(new LoggingFeature(
    logger,
    Level.ALL,
    LoggingFeature.Verbosity.PAYLOAD_ANY,
    8192))
  .request()
  .get();

Technically the Level.All and 8192 values could be null. I just provide them here to be concise.

樱花细雨 2024-12-03 22:45:38

对于 3.1.X 版本,它必须是:

Logger logger = Logger.getLogger("test");
LoggingFeature loggingFeature = new LoggingFeature(logger, Level.INFO, Verbosity.PAYLOAD_ANY,null);
Client client = ClientBuilder.newClient();
client.register(loggingFeature);

Level.ALL is NOTworking

For version 3.1.X it must be:

Logger logger = Logger.getLogger("test");
LoggingFeature loggingFeature = new LoggingFeature(logger, Level.INFO, Verbosity.PAYLOAD_ANY,null);
Client client = ClientBuilder.newClient();
client.register(loggingFeature);

Level.ALL is NOT Working

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文