Dropwizard:设置servlet上下文参数

时间:2012-11-06 21:25:18

标签: servlets dropwizard

给出以下web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <filter>
        <filter-name>Guice Filter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Guice Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>com.foo.JerseyContextListener</listener-class>
    </listener>
    <context-param>
        <param-name>module</param-name>
        <param-value>com.foo.MainModule</param-value>
    </context-param>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

如何告诉DropWizard将“module”servlet上下文参数设置为“com.foo.MainModule”?

Configuration.getHttpConfiguration().getContextParameters()始终返回一个空列表。我们应该延长这个课程吗?

2 个答案:

答案 0 :(得分:5)

您可以在配置文件中设置它:

- http:
  - contextParameters:
    - module: com.foo.MainModule

答案 1 :(得分:1)

您似乎需要覆盖Configuration.http

public class MyConfiguration extends Configuration
{
    public MyConfiguration()
    {
        this.http = new HttpConfiguration()
        {
            @Override
            public ImmutableMap<String, String> getContextParameters()
            {
                return ImmutableMap.<String, String>builder().put("module", MainModule.class.getName()).
                    build();
            }
        };
    }
}