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

时间:2013-08-15 19:29:39

标签: java jersey guice

我正在尝试启用跟踪来调试Jersey WS调用。我尝试在我的Guice servlet配置和web.xml中添加描述here的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>

3 个答案:

答案 0 :(得分:3)

如果要在服务器日志中看到日志消息(您的第一个跟踪配置是正确的),则需要在配置中添加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。

答案 1 :(得分:1)

发布的代码工作正常。我在日志中寻找跟踪消息。我没有意识到跟踪消息是在响应头中发回的,如here所述。

答案 2 :(得分:0)

同样的事情,但有一个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所述的属性。