繁体   English   中英

在使用Guice servlet配置时,如何在Jersey WS调用中启用跟踪

[英]How to turn on tracing in Jersey WS calls, when using Guice servlet configuation

我正在尝试启用跟踪来调试Jersey WS调用。 我尝试在我的Guice servlet配置和web.xml中添加这里描述的init-param,但似乎无法使其工作。

这是Guice servlet配置的样子:

public class GuiceServletConfig extends GuiceServletContextListener
{
private static final Logger log = LoggerFactory.getLogger(GuiceServletConfig.class);

public GuiceServletConfig()
{
    super();
    log.debug("creating GuiceServletConfig");
}



private static class ServletModule extends JerseyServletModule {
    private InputStream getInputStream() throws FileNotFoundException {
        File file = new File("tmp");
        String pathToTempFile = file.getAbsolutePath();
        log.debug("path to config: {}", pathToTempFile);
        String pathWithoutTmp = pathToTempFile.substring(0,
            pathToTempFile.indexOf(File.separator + "tmp"));
        StringBuilder stringBldr = new StringBuilder(pathWithoutTmp)
            .append(File.separator).append("extensions")
            .append(File.separator).append("__lib__")
            .append(File.separator).append("gelato.config.properties");
        log.debug("loading guice properties from: {}",  stringBldr.toString());
        return new FileInputStream(new File(stringBldr.toString()));
    }

    @Override
    protected void configureServlets() {
        Properties properties = new Properties();
        try {
            InputStream is = getInputStream();
            properties.load(is);
            Names.bindProperties(binder(), properties);
        } catch (Exception ex) {
            log.error("Error binding properties: {}", ex.toString());
        }
        // Must configure at least one JAX-RS resource or the
        // server will fail to start.
        // Must configure at least one JAX-RS resource or the
        // server will fail to start.
        bind(UserResource.class);
        bind(PlayersManager.class).to(GelatoPlayersManager.class);
        bind(MessageHandler.class).to(SFSMessageHandler.class);
        Map<String, String> params = new HashMap<String, String>();
        params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "org.buffalo.gelato.resources");
        // Route all requests through GuiceContainer
        params.put("com.sun.jersey.config.feature.Trace", "true");
        serve("/rest/*").with(GuiceContainer.class, params);
    }
}

我也尝试将它放在web.xml中,但我想这会被忽略,因为我通过Guice配置了Jersey。

<servlet>
    <servlet-name>Jersey REST Service for value codes</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.feature.Trace</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

如果要在服务器日志中看到日志消息(您的第一个跟踪配置是正确的),则需要在配置中添加ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS和/或ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS属性,如:

params.put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, com.sun.jersey.api.container.filter.LoggingFilter.class);
params.put(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, com.sun.jersey.api.container.filter.LoggingFilter.class);

请参阅容器LoggingFilter的 JavaDoc。

发布的代码工作正常。 我在日志中寻找跟踪消息。 我没有意识到所描述的消息在响应头传回的痕迹在这里

同样的事情,但有一个ResourceConfig文件。

web.xml

<servlet>
    <servlet-name>com.example.myapp</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.example.myapp.JerseyApplication</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>com.example.myapp</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

并在代码中:

package com.example.myapp;

import org.glassfish.jersey.server.ResourceConfig;
import java.util.HashMap;
import java.util.Map;

public class JerseyApplication extends ResourceConfig {
    public JerseyApplication() {
        System.out.println("Processing Jersey2 configuration");

        // Tracing properties (modification of the response HTTP headers)
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("jersey.config.server.tracing.type","ALL");
        params.put("jersey.config.server.tracing.threshold","TRACE");
        addProperties(params);

        // Scan packages for resources
        packages(true,"com.example.myapp.resources");
    }
}

如果您想要服务器端的跟踪,只需添加上面Michal Gajdos所述的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM