문제

I am developing a JAX-RS service that is deployed to IBM Websphere 8.5. I am trying to get the ServletContext injected by using the following code (currently at field level in the class, but I have also tried passing in as a parameter in the service method but I get the same problem).

@Context
private ServletContext servletContext = null;

When I invoke the my service method, the servletContext variable is null.

My web.xml file is setup as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
    <servlet-name>MyServices</servlet-name>
    <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.mycompany.ApplicationConfig</param-value>
    </init-param>
            <init-param>
        <param-name>myParam</param-name>
        <param-value>myValue</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>MyServices</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Then in the code I am using the servletContext to get an init-param, for example:

String myParam = servletContext.getInitParameter("myParam");

I have the IBM supplied j2ee.jar file (for Java 6 which is what is running) as a dependency shipped with the WAR file.

So my question is, is this a bug with Websphere's JAX-RS implementation, or am I missing something here?

I have found several guides on how to write JAX-RS applications for Websphere but none of them mention injecting the ServletContext. There is also no reference on how to do this in the IBM Websphere 8.5 info centre (that I can see at least).

도움이 되었습니까?

해결책

First of all, you should avoid using dated servlet specification of version 2.4 with JAX-RS. Even version 2.5 has some nasty limitations related to the injection of dependencies into JAX-RS beans.

Since you are using WAS 8.5, I suggest switching to Servlets 3.0 by updating web.xml.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top