我想在我的 web.xml 中有一些初始化参数,并稍后在应用程序中检索它们,我知道当我有一个普通的 servlet 时我可以做到这一点。然而,使用resteasy,我将HttpServletDispatcher配置为我的默认servlet,所以我不太确定如何从我的休息资源访问它。这可能非常简单,或者我可能需要使用不同的方法,无论哪种方式,了解你们的想法都会很好。以下是我的 web.xml,
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>RestEasy sample Web Application</display-name>
<!-- <context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param> -->
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.pravin.sample.YoWorldApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
我的问题是如何在 init-param 中设置某些内容,然后在静态资源中检索它。任何提示将不胜感激。多谢你们!
使用 @Context 注释将您想要的任何内容注入到您的方法中:
@GET
public Response getWhatever(@Context ServletContext servletContext) {
String myParm = servletContext.getInitParameter("parmName");
}
使用@Context,您可以注入 HttpHeaders、UriInfo、Request、HttpServletRequest、HttpServletResponse、ServletConvig、ServletContext、SecurityContext。
或者如果您使用此代码,则可以执行其他任何操作:
public class MyApplication extends Application {
public MyApplication(@Context Dispatcher dispatcher) {
MyClass myInstance = new MyClass();
dispatcher.getDefautlContextObjects().
put(MyClass.class, myInstance);
}
}
@GET
public Response getWhatever(@Context MyClass myInstance) {
myInstance.doWhatever();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)