java 使用 jersey ServletContainer 时从 web.xml 获取配置数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17324075/
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
Get configuration data from web.xml when using a jersey ServletContainer
提问by tallseth
I'm creating a webapp in Tomcat using jersey. I haven't created a Servlet, I just use the jersey ServletContainer and some Resource classes.
我正在使用 jersey 在 Tomcat 中创建一个 webapp。我还没有创建 Servlet,我只是使用了 jersey ServletContainer 和一些 Resource 类。
My web.xml:
我的 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mycompany.myproduct.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
My webapp needs to read some configuration values. I have the impression that a good way to do this is with context-Params, like this:
我的 webapp 需要读取一些配置值。我的印象是使用上下文参数是一个很好的方法,如下所示:
<web-app>
...
<context-param>
<description>This is a context parameter example</description>
<param-name>ContextParam</param-name>
<param-value>ContextParam value</param-value>
</context-param>
</web-app>
Is this the best way? How can I access these context params from my resource classes?
这是最好的方法吗?如何从我的资源类访问这些上下文参数?
Here's an example resource class:
这是一个示例资源类:
@Path("/api/ping")
public class PingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String helloWorld() {
return "pong";
}
}
回答by Jeff Storey
You can inject the ServletContext
and look up the parameters from there. Something like:
您可以ServletContext
从那里注入并查找参数。就像是:
public class PingResource {
@Context ServletContext context;
public String myServiceMethod() {
context.getInitParam("ContextParam");
}
}
回答by Arun Kumar
Below is snapshot that worked for me :)
以下是对我有用的快照:)
// add imports
// 添加导入
import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
//add property in your class
//在你的类中添加属性
@Context
ServletContext context;
// Use the context param in your methods
// 在你的方法中使用上下文参数
String companyName = this.context.getInitParameter("companyName");
// add context to web.xml
// 将上下文添加到 web.xml
<context-param>
<description>Context Parameter Test</description>
<param-name>companyName</param-name>
<param-value>Test Organization, Incorporated</param-value>
</context-param>