简体   繁体   English

迁移到泽西2.x现在我得到servlet容器异常

[英]Migrated to jersey 2.x and now I get servlet container exception

So I'm building a small rest api with java using Jersey 2.23.2. 所以我正在使用Jersey 2.23.2用java构建一个小的rest api。 I have run into a problem however, I am not able to specify the servlet in the web.xml file properly. 我遇到了一个问题,但我无法正确指定web.xml文件中的servlet。 This is how my files look like: 这就是我的文件的样子:

pom.xml 的pom.xml

<dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.23.2</version>
        </dependency>       
        <dependency>
          <groupId>org.glassfish.jersey.containers</groupId>
          <artifactId>jersey-container-servlet-core</artifactId>
          <version>2.23.2</version>
          <scope>provided</scope>
        </dependency>
    </dependencies>

web.xml web.xml中

...
  <servlet>
        <servlet-name>WebService</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
             <param-value>api-mashup-api.API</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>WebService</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

</web-app>

API.java API.java

package api;
    @Path("/v1")
    public class API {
    private Controller controller;

    public API(){
        controller = new Controller();
    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String print1() {
        return "Please specify what resource you need.";
    }

    /**
     * Prints to the screen if we are in /v1/foo
     * 
     * @return
     */
    @GET
    @Path("/foobar")
    @Produces(MediaType.TEXT_HTML)
    public String print2() {
        return "Please specify what resource you need.";
    }
}

My project folder structure looks like this: 我的项目文件夹结构如下所示:

api-mashup-api/Java Resources/src/api/API.java

I used Jersey 1.x before and then I migrated to 2.x and now I'm not sure what to put into the params of the servlet in web.xml. 之前我使用过Jersey 1.x然后我迁移到2.x现在我不确定在web.xml中将什么放入servlet的params中。 When I try to run the API.java on my tomcat server I get the following exception: 当我尝试在我的tomcat服务器上运行API.java时,我得到以下异常:

SEVERE: Servlet [WebService] in web application [/api-mashup-api] threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

I can reach my index.html just fine though, so the server works, but not the API part. 我可以很好地到达我的index.html,所以服务器工作,但不是API部分。 Not sure what to do here. 不知道该怎么做。 Obviously something is wrong with the servlet part. 显然servlet部分出了问题。

EDIT-Things I have tried: 编辑 - 我尝试过的事情:

I've had and fixed this error many times. 我已经多次修复了这个错误。 It's basically always a dependency issue. 它基本上总是一个依赖问题。

Here are the steps to fix it: 以下是修复它的步骤:

  1. Make sure you're using the most recent version of all packages (ie 2.23.2 of glassfish) and servers, updating your POM accordingly. 确保您使用的是最新版本的所有软件包(即2.23.2的glassfish)和服务器,并相应地更新您的POM。 This is very important for your case since you're using the new version of Jersey. 这对您的案例非常重要,因为您使用的是新版本的Jersey。

  2. Refresh your project 刷新您的项目

  3. Use the Maven menu to clean and build 使用Maven菜单清理和构建

  4. Use the Maven menu to refresh (this is different from the project refresh) 使用Maven菜单刷新(这与项目刷新不同)

Don't skip steps or assume that if you've done some of them here and there in the past that it's the same as doing them one after the other. 不要跳过步骤或假设如果你过去在这里和那里做过其中的一些,那就像一个接一个地做它们一样。

Probably issue with servlet container "jersey-container-servlet-core" is for servlet 2.x containers, try changing to 可能与servlet容器“jersey-container-servlet-core”的问题是针对servlet 2.x容器,尝试更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM