如何在tomcat上获取Jersey 2.5.1来记录请求/响应,包括实体(请求/响应正文)

时间:2014-01-24 09:15:53

标签: tomcat jersey-2.0

我在tomcat上部署了Jersey 2.5.1应用程序。我在web.xml中添加了以下配置:

<init-param>
  <param-name>jersey.config.server.provider.classnames</param-name>
  <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>

这是打印请求和响应标头。但是,它不会在响应中打印请求/响应实体/主体。

我试过玩不同的参数,但没有运气,比如:

INFO: 1 * Server has received a request on thread http-bio-8080-exec-3
1 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
1 > accept-encoding: gzip,deflate,sdch
1 > accept-language: en-US,en;q=0.8
1 > cache-control: no-cache
1 > connection: keep-alive
1 > cookie: JSESSIONID=1F5CCACA7DF2EABA9475C270A95C91B2
1 > host: localhost:8080
1 > pragma: no-cache
1 > user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36


Jan 24, 2014 1:01:31 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Server responded with a response on thread http-bio-8080-exec-3
1 < 200
1 < Content-Type: application/json

感谢您的回答。看起来我上面贴了一个错误的配置。这是我在web.xml中的内容:

  <servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.my.package.web.resources;org.codehaus.jackson.jaxrs</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.tracing</param-name>
        <param-value>ALL</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

记录标题。但它不记录实体。这是我在日志中看到的内容:

Jan 28, 2014 2:36:38 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 5 * Server has received a request on thread http-bio-8080-exec-10
5 > GET http://localhost:8080/sample/api/foo
5 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
5 > accept-encoding: gzip,deflate,sdch
5 > accept-language: en-US,en;q=0.8
5 > cache-control: max-age=0
5 > connection: keep-alive
5 > cookie: JSESSIONID=1F5CCACA7DF2EABA9475C270A95C91B2
5 > host: localhost:8080
5 > user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36

Jan 28, 2014 2:36:38 PM Jan 28, 2014 2:36:38 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 5 * Server responded with a response on thread http-bio-8080-exec-10
5 < 200
5 < Content-Type: application/json

My Jersey应用程序部署在Tomcat 7_50服务器上。

如果我在web.xml中使用init-param配置了跟踪和loggingfilter。我还需要使用Recource Config注册吗?

公共类MyApplication扩展ResourceConfig,如下所示:

    public MyApplication() {
        // Register resources and providers using package-scanning.
        packages("my.package");

        // Register my custom provider - not needed if it's in my.package.
        register(SecurityRequestFilter.class);
        // Register an instance of LoggingFilter.
        register(new LoggingFilter(LOGGER, true));

        // Enable Tracing support.
        property(ServerProperties.TRACING, "ALL");
    }
}

2 个答案:

答案 0 :(得分:1)

  1. 您无法在web.xml
  2. 中对其进行配置
  3. 你混合了两个版本的泽西岛(web.xml中的init-params来自泽西岛1 - com.sun.jersey包裹前缀)
  4. 看看Registering Resources and Providers in Jersey 2,您可以在那里找到如何注册LoggingFilter以确保实体也被记录。

答案 1 :(得分:0)

您可以根据Lokesh Gupta instructions

创建public class MyApplication extends Application { @Override public Set<Class<?>> getClasses() { final Set<Class<?>> classes = new HashSet<>(); classes.addAll(super.getClasses()); classes.add(CustomLoggingFilter.class); return classes; } }

我最终注册过滤器的方式略有不同但是:

MyApplication.java

web.xml

中注册<init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.something.MyApplication</param-value> </init-param>
[{
      'id': 1,
      'first_name': 'Joseph',
      'last_name': 'Williams',
      'email': 'jwilliams0@hud.gov',
      'gender': 'Male',
      'age': 94
    }, {
      'id': 2,
      'first_name': 'Christina',
      'last_name': 'Lawrence',
      'email': 'clawrence1@clickbank.net',
      'gender': 'Female',
      'age': 67
    }]