3

我正在尝试在 NetBeans 7.4(今天发布)和 GlassFish 4 服务器下开发 JAX-RS。

显然,NetBeans 7.4 应该使用 Jersey 2.0,但是查看项目中的 library->GlassFish Server 下的库,我只看到几个通用 jar:

  • javax.servlet-api.jar
  • javax.ws.rs-api.jar
  • ...

所以我不确定泽西岛和服务器使用的是哪个版本。所以第一个问题是:我是否需要在运行时使用与编译时相同的 JAX-RS 实现?我怎么知道我在 NetBeans 7.4 下使用的是什么版本?

现在使用 com.sun.jersey.spi.container.servlet.ServletContainer 作为 web.xml 中的 Servlet 类返回

SEVERE:   WebModule[/TestApplication]Error loading WebappClassLoader (delegate=true; repositories=WEB-INF/classes/) com.sun.jersey.spi.container.servlet.ServletContainer

在使用 org.glassfish.jersey.spi.container.servlet.ServletContainer 时

SEVERE:   WebModule[/TestApplication]Error loading WebappClassLoader (delegate=true; repositories=WEB-INF/classes/) org.glassfish.jersey.spi.container.servlet.ServletContainer

所以第二个问题是:什么是正确的 Servlet 类?

在 WEB-INF/lib 下添加从https://jersey.java.net/download.html下载的 Jersey 2.0库,并使用后者的 Servlet 类产生:

[2013-10-15T18:05:45.136+0200] [glassfish 4.0] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=35 _ThreadName=admin-listener(5)] [timeMillis: 1381853145136] [levelValue: 1000] [[
  Exception while loading the app : CDI deployment failure:WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref<ContainerRequest>, ProcessingProviders)]
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref<ContainerRequest>, ProcessingProviders)]
        at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:403)
        at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:325)
        at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:177)
        at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:208)
        at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:519)
        at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:505)
        at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:480)
        at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:536)
        at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:216)
        at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
        at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:328)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:493)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
        at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:527)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:356)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
        at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
        at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
        at org.glassfish.grizzly.http.server.StaticHttpHandler.service(StaticHttpHandler.java:297)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
        at java.lang.Thread.run(Thread.java:722)
]]

编辑:

从 WEB-INF 中的 lib/ 文件夹中删除库(如 Michal Gajdos 建议的那样)可以避免运行时错误。因此 web.xml 现在是

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/webresources/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

但是 JAX-RS 仍然不想启动,并且服务器日志上没有显示任何内容。我还缺少其他指令吗?

撞!

4

2 回答 2

4

Jersey 2.x 中的正确HttpServlet类是

org.glassfish.jersey.servlet.ServletContainer

改变你web.xml使用这个,你应该没问题。

注意:您的应用程序不应包含已存在于 GF 的类路径中的库(它可能会导致与您的类似的异常)。

于 2013-10-15T17:53:08.927 回答
1

Jersey 2 完全改变了工作方式,请参阅jersey 部署文档 CHPT 4

首先,不再有 web.xml 配置,删除您在其中添加的所有内容,因为不再需要它来启动 Jersey 服务器。您只需要添加

@ApplicationPath("webresources")
@Path("/people")
@Stateless
public class PersonResource extends Application
{
 ...
 @GET
    @Path("/getpeople")
    @Produces(
    {
        MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
    })
  ...

如您所见,javax.ws.rs.ApplicationPath 中定义的@ApplicationPath 替代了之前的 web xml 根路径配置,并且 Resource 类必须扩展 javax.ws.rs.core.Application 中定义的应用程序。

就是这样,实际上 NetBeans 7.4 在这方面并没有太大帮助,即使使用错误的 web.xml 也会编译,但不幸的是没有任何提示。

于 2013-10-16T08:06:35.917 回答