我正在尝试创建一个自包含Web服务,其中包含战争中包含的所有jersey依赖项以部署到Weblogic。我想更喜欢战争中的版本和Weblogic默认提供的版本。我已经启动并运行了Web服务,但出于某种原因,在将带有@EJB
的EJB注入JAX-RS资源(无状态EJB本身)时,EJB为空。
以下是如何重现此内容的示例。
我的pom依赖
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.4</version>
</dependency>
web.xml
<web-app version="2.5" 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">
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.example.mavenscratchservice
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
weblogic.xml中
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app" 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 http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">
<jsp-descriptor>
<keepgenerated>true</keepgenerated>
<debug>true</debug>
</jsp-descriptor>
<container-descriptor>
<prefer-web-inf-classes>false</prefer-web-inf-classes>
<prefer-application-packages>
<package-name>javax.ws.rs.*</package-name>
<package-name>com.fasterxml.jackson.*</package-name>
<package-name>org.glassfish.jersey.*</package-name>
<package-name>org.glassfish.hk2.*</package-name>
<package-name>org.jvnet.*</package-name>
<package-name>jersey.repackaged.*</package-name>
</prefer-application-packages>
<prefer-application-resources>
<resource-name>META-INF/services/javax.ws.rs.ext.*</resource-name>
<resource-name>META-INF/services/org.glassfish.jersey.internal.*</resource-name>
<resource-name>META-INF/services/org.glassfish.jersey.servlet.*</resource-name>
<resource-name>META-INF/services/org.glassfish.hk2.*</resource-name>
<resource-name>META-INF/services/java.servlet.ServletContainerInitializer</resource-name>
<resource-name>META-INF/services/com.fasterxml.jackson.*</resource-name>
</prefer-application-resources>
</container-descriptor>
<context-root>helloworld</context-root>
</weblogic-web-app>
JAX-RS资源
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("rest")
@Stateless
public class ScratchResource
{
@EJB
private HelloWorldService helloWorldService;
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response helloWorld()
{
return Response.ok(helloWorldService.helloWorld()).build();
}
}
EJB
import javax.ejb.Stateless;
@Stateless
public class HelloWorldService
{
public String helloWorld()
{
return "Hello world";
}
}
最后我收到的堆栈跟踪。我已经尝试将资源更改为CDI bean,我甚至尝试使用@EJB
的查找属性通过它的JNDI直接引用EJB。
]] Root cause of ServletException.
java.lang.NullPointerException
at com.example.mavenscratchservice.ScratchResource.helloWorld(ScratchRe
ource.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl
java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcce
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationH
ndlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMetho
Dispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMetho
Dispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatc
erProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.j
va:160)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMetho
Dispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(Resou
ceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(Resour
eMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(Resour
eMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:3
6)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(Reques
Scope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java
305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHan
ler.java:1154)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.j
va:473)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:
27)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContain
r.java:388)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContain
r.java:341)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContain
r.java:228)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.ru
(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.ru
(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecur
tyHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.ja
a:346)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.j
va:79)
at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(Conver
ationPropagationFilter.java:62)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.j
va:79)
at oracle.security.jps.ee.http.JpsAbsFilter$1.run(JpsAbsFilter.java:137
at java.security.AccessController.doPrivileged(Native Method)
at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:3
5)
at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformU
il.java:460)
at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.ja
a:120)
at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:
17)
at oracle.security.jps.ee.http.JpsFilter.doFilter(JpsFilter.java:81)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.j
va:79)
at oracle.dms.servlet.DMSServletFilter.doFilter(DMSServletFilter.java:2
0)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.j
va:79)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEvents
ilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.j
va:79)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationActi
n.wrapRun(WebAppServletContext.java:3436)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationActi
n.run(WebAppServletContext.java:3402)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Authenticat
dSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java
120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java
57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebA
pServletContext.java:2285)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebApp
ervletContext.java:2201)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServlet
ontext.java:2179)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.
ava:1572)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExe
utor.run(ContainerSupportProviderImpl.java:255)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)