【问题标题】:Unable to run Jersey 2 Restful web service on Tomcat 9无法在 Tomcat 9 上运行 Jersey 2 Restful Web 服务
【发布时间】:2018-08-26 22:21:49
【问题描述】:

我正在编写 Jersey 2 Restful 网络服务。 这是服务类:

package com.Test.PS;
import java.io.IOException;
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.QueryParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;

import com.Test.Exchange.*; // Here class UserInfo is defined

@Path("/ps")
public class TestService {
    private UserInfo ui;
    public TestService () throws IOException {
        ui = new UserInfo();
    }
    public TestService (String uid) throws IOException {
        UserInfo ui = ObjectFileStore.serializeDataIn(uid);
    }

    public TestService (UserInfo ui) throws IOException {
        this.ui = ui;
        ObjectFileStore.serializeDataOut(ui);
    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHelloHTML(@QueryParam("uid") String uid) {     
        String resource="<h1> Hi '" + uid + "'. </h1>";
        return resource;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public UserInfo postNK(@QueryParam("asid") String asid, UserInfo u) {
        return ui;
    }
}

这里是maven依赖:

<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-server</artifactId>
  <version>2.27</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-client</artifactId>
  <version>2.27</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-servlet</artifactId>
  <version>2.27</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.glassfish.jersey.inject</groupId>
  <artifactId>jersey-hk2</artifactId>
  <version>2.27</version>
  <scope>provided</scope>
</dependency>

最后,这是我的 web.xml 文件:

  <display-name>Test-PS</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>Test-PS</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.Test.PS</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Test-PS</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

使用 url (http://localhost:8081/Test-PS/rest/ps?uid=90) 在 Tomcat 9 上运行此服务时出现以下错误:

HTTP Status 404 – Not Found
Type Status Report
Message /Test-PS/rest/ps
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

在控制台屏幕上,我看到:

INFO: Reloading Context with name [/Test-PS] has started
org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/Test-PS] is completed

我尝试按照此处其他帖子的建议添加其他几个依赖项,但是仍然遇到同样的问题。令人惊讶的是,有时它会出于未知原因工作!!!

我也删除了所有的maven仓库,没有任何改变!!!

【问题讨论】:

    标签: rest jersey-2.0 tomcat9


    【解决方案1】:

    使用 jersey-container-servlet-core 而不是 jersey-container-servlet

    参考:

    泽西岛依赖 - https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/modules-and-dependencies.html

    Tomcat 版本比较 - http://tomcat.apache.org/whichversion.html

    【讨论】:

    • 你刚刚让我头疼了!
    【解决方案2】:
    1. 从所有 Jersey 依赖项中删除 &lt;scope&gt;provided&lt;/scope&gt;。仅当服务器的库中已包含这些 jar 时,您才使用它。 Tomcat 没有 Jersey 罐子。另一方面,如果您使用的是 Glassfish,那么您可以使用 provided,因为 Glassfish 已经拥有所有 Jersey jars。

    2. 删除 Jersey-clientjersey-server 依赖项。他们都已经被jersey-container-servlet 拉进来了。不需要冗余。

    【讨论】:

    • 感谢您的回复.. 我之前尝试过,正如我在问题中所说的那样,我尝试了其他几种依赖组合,但我仍然面临同样的问题。但是,我尝试仅使用您建议的默认编译范围来维护 jersey-container-servlet、javax.servlet-api 和 jersey-hk2 依赖项。我仍然遇到同样的问题!!!
    • 我也尝试过 jersey-container-servlet-core,结果相同。我正在考虑改用玻璃鱼。看来 Jersey 的 2 个 glassfish 库与 Tomcat 9 不兼容
    • 如果想要使用不同版本的球衣(Tomcat 服务器中已经存在的除外),需要做什么?还是不能在现有的 Tomcat Server 中使用不同版本的球衣?
    【解决方案3】:

    问题通过以下方式解决:

    • 删除 Eclipse 并安装 EE 版本。
    • 删除 Tomcat 并安装 9.0.11 版本。
    • 从头开始重新创建项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2014-09-13
      相关资源
      最近更新 更多