Java 如何在服务器上获取球衣日志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2332515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:15:02  来源:igfitidea点击:

How to get jersey logs at server?

javajersey

提问by Fakrudeen

I am using jersey for a REST WS. How do I enable jersey logs at server side?

我正在将球衣用于 REST WS。如何在服务器端启用球衣日志?

Long story: I get a clientside exception - but I don't see anything in tomcat logs [It doesn't even reach my method]. Since the stack trace is saying "toReturnValue" it did get something from server. But I don't know what the server said.

长话短说:我收到一个客户端异常 - 但我在 tomcat 日志中没有看到任何内容 [它甚至没有达到我的方法]。由于堆栈跟踪说的是“toReturnValue”,它确实从服务器获取了一些信息。但是不知道服务员怎么说。

Exception in thread "main" java.lang.IllegalArgumentException: source parameter must not be null
 at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:98)
        at com.sun.xml.internal.ws.message.AbstractMessageImpl.readPayloadAsJAXB(AbstractMessageImpl.java:100)
        **at com.sun.xml.internal.ws.client.dispatch.JAXBDispatch.toReturnValue(JAXBDispatch.java:74)**
        at com.sun.xml.internal.ws.client.dispatch.DispatchImpl.doInvoke(DispatchImpl.java:191)
        at com.sun.xml.internal.ws.client.dispatch.DispatchImpl.invoke(DispatchImpl.java:195)

采纳答案by Brian Clozel

If you want to turn on logging on the server side, you need to register the LoggingFilter Jersey filter(on the container side).

如果要在服务器端开启日志记录,则需要注册LoggingFilter Jersey 过滤器(在容器端)。

This filter will log request/response headers and entities.

此过滤器将记录请求/响应标头和实体

Here's what you need to add to your ResourceConfigclass:

以下是您需要添加到ResourceConfig课程中的内容:

@ApplicationPath("/")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        // Resources.
        packages(MyResource.class.getPackage().getName());

        register(LoggingFilter.class);    
    }
}

Note that the same filter also works on the client side.

请注意,相同的过滤器也适用于客户端。

Client client = Client.create();
client.addFilter(new LoggingFilter());

回答by Brian Clozel

Could you show us your client code and tell us about the request as well?

您能否向我们展示您的客户代码并告诉我们有关请求的信息?

This exception seems to point at the JAXB unmarshalling step. Apparently you received some XML from your REST API, but you don't get what you're waiting for.

此异常似乎指向 JAXB 解组步骤。显然,您从 REST API 收到了一些 XML,但没有得到您所期待的。

Maybe the XSD you're using for marshalling/unmarshalling is outdated or just plain wrong.
Maybe you're trying to get the wrong entity from the response.

也许您用于编组/解组的 XSD 已过时或完全错误。
也许您正试图从响应中获取错误的实体。

Try these steps and give us some more details about your problem:

尝试以下步骤,并向我们提供有关您的问题的更多详细信息:

Get the XML from the response

从响应中获取 XML

Using a REST client like Client REST simple(a chrome extension), or your code:

使用像Client REST simple(一个 chrome 扩展)这样的 REST 客户,或者你的代码:

Builder builder = webResource.path("/yourapi/").accept("application/xml");

// get the client response
ClientResponse response = builder.get(ClientResponse.class);

// log the HTTP Status
logger.log("HTTP Status: " + response.getStatus());

// bypass the jaxb step and get the full response
// MyResource myResource = response.getEntity(MyResource.class);
String myResource = response.getEntity(String.class);
logger.log(myResource);

Validate this XML with the XSD you're using

使用您正在使用的 XSD 验证此 XML

This test should fail (if I'm right).

这个测试应该会失败(如果我是对的)。

回答by lanwen

Jersey 2.0 uses org.glassfish.jersey.filter.LoggingFilter
You can connect it with help of web.xml

Jersey 2.0 使用org.glassfish.jersey.filter.LoggingFilter
你可以在web.xml 的帮助下连接它

<!-- Register my custom provider (not needed if it's in my.package) AND LoggingFilter. -->
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
        </init-param>

More explanations can be found here

更多解释可以在这里找到

upd:

更新:

After version 2.23LoggingFilteris deprecated and LoggingFeatureshould be used. More info can be found in official documentation

2.23版本LoggingFilter被弃用并LoggingFeature应该使用之后。更多信息可以在官方文档中找到

回答by eeezyy

For Jersey 1.2 add the following entry into web.xmlinside the servlet tag:

对于 Jersey 1.2,web.xml在 servlet 标签中添加以下条目:

    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
    </init-param>

回答by Art

Jersey 2 has deprecated LoggingFilterand you now need to use LoggingFeature. In order to use it with a client you can use the following snipette:

Jersey 2 已弃用LoggingFilter,您现在需要使用LoggingFeature. 为了与客户端一起使用它,您可以使用以下代码:

this.client = ClientBuilder
            .newBuilder()
            .property(LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT, LoggingFeature.Verbosity.PAYLOAD_ANY)
            .property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_CLIENT, "WARNING")
            .build();

and on the server side:

在服务器端:

ResourceConfig config = new ResourceConfig(HelloWorldResource.class);
config.register(LoggingFeature.class);