【问题标题】:jetty+jersey servlet mapping not workingjetty+jersey servlet 映射不起作用
【发布时间】:2016-02-26 23:17:12
【问题描述】:

我正在尝试使用 jetty 和 jersey 实现 REST Web 服务。我创建了一个 maven webapp 项目,我也想在这个项目中使用 maven jetty 插件。

我现在面临的问题是 servlet 映射似乎不起作用。我希望将资源映射到我在 web.xml 中尝试过的http://localhost:8080/rest/test,但是在访问 URI 时我得到 404 not found。

这里是 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.testproject</groupId>
  <artifactId>rest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>rest Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.22.1</version>
</dependency>

    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.22.1</version>
</dependency>

    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.22.1</version>
</dependency>

    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-jetty-http</artifactId>
    <version>2.22.1</version>
</dependency>  
    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-jetty-servlet</artifactId>
    <version>2.22.1</version>
</dependency>


  </dependencies>

  <build>

  <plugins>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.3.6.v20151106</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>          
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>

      </plugins>
    <finalName>rest</finalName>
  </build>
</project>

RestResource.java

package com.testproject.rest.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/rest")
public class RestResource {

@GET
@Path("/test")
@Produces(MediaType.TEXT_PLAIN)
public String testService(){
    return "success!";
}

}

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>


    <servlet>
        <servlet-name>REST</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.testproject.rest.resource</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>REST</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>


</web-app>

我确实在谷歌搜索过这个问题,但我找到的解决方案似乎都不起作用。由于这是我第一次使用 Web 服务器和 Web 服务,所以我还没有完全理解整个主题。

【问题讨论】:

    标签: java servlets jetty maven-3 jersey-2.0


    【解决方案1】:

    把@Path("/rest")改成@Path("/test"),去掉方法上的其他@Path("/test")。每个未使用@Path 注释的方法都将转到类上的/test。

    /rest/* 已在 web.xml 的 servlet 映射中定义,因此它将是根。

    您目前如何拥有它,您需要访问它。 /rest/rest/test

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-14
      • 2013-04-26
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      相关资源
      最近更新 更多