如何获得Jersey 2.2(JAX-RS)生成日志输出,包括Json请求正文 [英] How do I get Jersey 2.2 (JAX-RS) to generate log output, including Json request bodies
问题描述
我正在Jetty 9.0.4内运行Jersey 2.2 Servlet,以便处理REST请求.
I'm running a Jersey 2.2 Servlet inside Jetty 9.0.4 in order to serve REST requests.
大多数情况都很好,可以满足请求,但是我从未见过Jersey类的 ANY 日志.而且我找不到任何doco来表明要实现泽西岛2.2的情况我需要牺牲哪些鸡
Mostly everything is good and requests get served, but I have never seen ANY log from Jersey classes. And I can't find any doco indicating what chickens I need to sacrifice to make that happen with Jersey 2.2
所以我的第一个问题是-要让Jersey生成一些日志,我需要做些什么.
So my first question is - what do I need to do to get Jersey to generate some log.
当请求确实运行不正确时(例如,因为无法解析Json请求主体),Jersey将抛出ContainerException,并显示诸如无法从START_OBJECT令牌中反序列化java.util.ArrayList的实例"之类的消息.要记录下传入的请求正文,这将真的很可爱,这样我就可以检查Json.同样,尽管我确定有一只野兽,但我在当前的doco中找不到能概述这种野兽的任何东西.无论如何,直到我解决问题1以上的问题为止.
When a request does run awry (eg because the Json request body can't be parsed) Jersey will throw a ContainerException with message like "Can not deserialize instance of java.util.ArrayList out of START_OBJECT token" etc. At that point it would be really lovely to have logged the incoming request body so I can inspect the Json. Again I can't find anything in the current doco outlining such a beast although I'm sure there is one. And in any case until I solve question 1 up above it's moot.
所以我的第二个问题是如何记录传入的请求正文(不中断请求).
So my 2nd question is how do I log the incoming request body (without disrupting the request).
web.xml中的Jersey Servlet配置如下:
The Jersey Servlet config in web.xml looks like:
<servlet >
<servlet-name>Jersey Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>au.com.xandar.wirelesstiming.recorder.web.rest.JerseyApplication</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>
<load-on-startup>1</load-on-startup>
</servlet>
我的JerseyApplication是:
My JerseyApplication is:
public final class JerseyApplication extends ResourceConfig {
public JerseyApplication() {
super(
//JacksonFeature.class // Switching on Jackson
// (My) JerseyLoggingFilter.class // Log requests using Jersey ContainerRequestFilter
MyApplicationEventListener.class // Log Requests using Jersey RequestEventListener
);
packages("au.com.xandar.wirelesstiming.recorder");
// Enable LoggingFilter & output entity.
// NB This does NOT generate any log.
registerInstances(new LoggingFilter(Logger.getLogger(JerseyApplication.class.getName()), true));
}
}
推荐答案
Jersey本身的日志并不多.对于2.x,我们正在开发一种开发模式,其中将包括跟踪支持(请参见
Jersey doesn't log much by itself. For 2.x we're working on a development mode which would include tracing support (see Tracing in Jersey), default logging of incoming requests / outgoing responses (incl. entity bodies and tracing messages). This feature should be out soon.
关于第二个问题-您可以注册 LoggingFilter 能够记录传入的请求/传出的响应(方法,标头,..)以及实体主体(配置时).您可以通过(第三个选项说明如何打开实体日志记录)进行配置:
Regarding your second question - you can register LoggingFilter which is able to log incoming requests / outgoing responses (method, headers, ..) as well as entity bodies (when configured). You can configure it via (3rd option illustrates how to turn-on entity logging):
web.xml
(将其添加到JAX-RS servlet定义中):
web.xml
(add it to the JAX-RS servlet definition):
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>
Application
扩展名:
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>() {{
// Add your resources.
add(HelloWorldResource.class);
// Add LoggingFilter.
add(LoggingFilter.class);
}};
}
}
ResourceConfig
实例(在此也演示了实体的输出):
ResourceConfig
instance (demonstrating also outputting the entity here):
public class MyApplication extends ResourceConfig {
public MyApplication() {
// Resources - add your package name here to enable package scanning.
packages(...);
// Enable LoggingFilter & output entity.
registerInstances(new LoggingFilter(Logger.getLogger(MyApplication.class.getName()), true));
}
}
这篇关于如何获得Jersey 2.2(JAX-RS)生成日志输出,包括Json请求正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!