web.xml for jersey servlets没有响应请求

时间:2015-09-08 04:28:23

标签: java eclipse rest maven jersey

您好我尝试使用jersey 1.8创建REST Web服务。我在服务器上运行。和服务器成功启动。但是请求没有进入服务类。浏览器显示404错误。我的web.xml是

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>MavenSample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
   </welcome-file-list>
   <servlet>
    <servlet-name>jersey</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
       <init-param>
          <param-name>com.sun.jersey.config.property.packages</param-name> 
          <param-value>java.com.mine.rest.HelloWorldServices</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
</servlet>

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


</web-app>

我的休息服务类是

package com.mine.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("service")
public class HelloWorldServices {

     @GET
     @Path("hello")
      @Produces("text/html")
      public Response getLocalCust() {

               String output = "Hello marvel , welcome to Rest Web service ";
               return Response.status(200).entity(output).build();
      }



}

我的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>MavenSample</groupId>
  <artifactId>MavenSample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

          <dependencies>
                <dependency>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-server</artifactId>
                    <version>1.8</version>
                </dependency>
          </dependencies>

</project>

请帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:1)

java.com.mine.rest.HelloWorldServices不是套餐。 web.xml中的属性值int应为com.mine.rest

<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name> 
  <param-value>com.mine.rest</param-value>
</init-param>

这将扫描com.mine.rest包及其子包中的所有类,以查找使用@Path注释的所有资源类