且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JEE 无法运行 JAX-RS WebService 框架应用程序

更新时间:2022-06-10 05:56:59

Glassfish 4 使用 Jersey 2.x.您应该相应地更改依赖项和 web.xml 配置.对于依赖,你可以使用

Glassfish 4 uses Jersey 2.x. You should change the dependency and web.xml configuration accordingly. For the dependency, you can use

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>${jersey2.version}</version>
    <scope>provided</scope>
</dependency>

和配置

<servlet>
    <servlet-name>jersey-serlvet</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.wld.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

最近看到很多人(使用 Glassfish 和 Jersey)的一个问题"是 Glassfish 使用的是旧版本的 Jersey(旧的 2.x 版本).目前最新的是2.17.Glassfish 使用了与 2.x 系列中的第一个类似的东西.因此,新功能似乎存在一些兼容性问题.

One "gotcha" a see a lot of people (who are using Glassfish and Jersey) lately is the fact that Glassfish uses an older version of Jersey (old in the sense of an older 2.x version). The latest as of now is 2.17. Glassfish uses something near the first one in the 2.x family. So there seems to be some compatibility problems with new features.

我肯定会研究的一件事是 在 GlassFish 4 中更新 Jersey 2.这是一篇相当不错的文章.对于简单的启动应用程序,您可能不会遇到任何问题,但是对于新功能,尝试在 Glassfish 中升级 Jersey 可能会有所帮助.

One thing I would definitely look into is Updating Jersey 2 in GlassFish 4. It's a pretty good article. You may not face any issues will simple startup apps, but with new features, trying to upgrade Jersey in Glassfish might help.

还有一件事,记得摆脱你的 Jersey 1.x 依赖项.两者完全不相容.还要注意 Maven 依赖项中的 provided 范围.由于 Glassfish 已经内置了 Jersey,所以我们只是使用内部依赖项.

Another thing, remember to get rid of your Jersey 1.x dependencies. The two are completely incompatible. Also notice the provided scope in the Maven dependency. Since Glassfish has Jersey built in already, we are just using the internal dependencies.

Jersey 2.x Documentation

哦,还有一件事.Jersey 有一个原型,可以帮助您轻松入门.在 Netbeans 中就可以了

Oh and one other thing. Jersey has an archetype that helps you get started easily. In Netbeans just do

  1. 新项目
  2. Maven →Archetype 的项目
  3. 搜索 jersey-quickstart-webapp
  4. 选择组 ID org.glassfish.jersey.archetypes
  5. 的那个
  6. 应显示最新版本.