Eclipse, maven, Jersey REST service, servlet 3.0, Tomcat 7.0, HTTP 404 Error -


i know title looks many people have asked question before, did lot of research , couldn't find solution solves problem. i've spent 2 days on issue please me out!

i trying build simple "hello world" type of restful web service eclipse kepler service release 2, jersey, eclipse build-in maven, servlet 3.0 , tomcat 7.0. when run service using "run as" --> "run on server", there no problem loading default "index.jsp" page, keep getting http 404 error when loading service.

this image shows project layout.

my class implements application following:

package com.ys.java.ws;  import java.util.hashset; import java.util.set;  import javax.ws.rs.applicationpath; import javax.ws.rs.core.application;  import com.ys.java.ws.resources.myservice;  @applicationpath("/ys") public class myapplication extends application {     @override     public set<class<?>> getclasses() {         set<class<?>> s = new hashset<class<?>>();         s.add(myservice.class);         return s;     }  } 

my resource class is:

package com.ys.java.ws.resources;  import javax.ws.rs.get; import javax.ws.rs.path; import javax.ws.rs.produces;  @path("/test") public class myservice {      public myservice() {      }      @get     @path("/hello")     //@produces(mediatype.text_plain)     @produces("text/html")     public string gettestmsg() {         return "it works";     } } 

my pom.xml file is:

<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.ys.java.ws</groupid>     <artifactid>ysnowebxml</artifactid>     <packaging>war</packaging>     <version>0.0.1-snapshot</version>     <name>ysnowebxml 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>         <!-- servlet 3.0 api -->         <dependency>             <groupid>javax.servlet</groupid>             <artifactid>javax.servlet-api</artifactid>             <version>3.0.1</version>         </dependency>         <!--jax-rs web service api -->         <dependency>             <groupid>org.glassfish.jersey.core</groupid>             <artifactid>jersey-server</artifactid>             <version>2.23.1</version>         </dependency>         <dependency>             <groupid>org.glassfish.jersey.core</groupid>             <artifactid>jersey-common</artifactid>             <version>2.23.1</version>         </dependency>         <dependency>             <groupid>org.glassfish.jersey.containers</groupid>             <artifactid>jersey-container-servlet</artifactid>             <version>2.23.1</version>         </dependency>         <dependency>             <groupid>org.glassfish.jersey.containers</groupid>             <artifactid>jersey-container-servlet-core</artifactid>             <version>2.23.1</version>         </dependency>     </dependencies>     <build>         <finalname>testservice</finalname>         <plugins>             <plugin>                 <groupid>org.apache.maven.plugins</groupid>                 <artifactid>maven-compiler-plugin</artifactid>                 <version>3.3</version>                 <configuration>                     <source>1.7</source>                     <target>1.7</target>                 </configuration>             </plugin>         </plugins>     </build> </project> 

my web.xml is:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"          xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"          xsi:schemalocation="http://java.sun.com/xml/ns/javaee            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"     version="3.0">     <display-name>servlet 3.0 web application</display-name>     <servlet>         <servlet-name>com.ys.java.ws.myapplication</servlet-name>     </servlet>     <servlet-mapping>         <servlet-name>com.ys.java.ws.myapplication</servlet-name>         <url-pattern>/rest</url-pattern>     </servlet-mapping> </web-app> 

note that: in myapplication class, set @applicationpath("/ys") , in web.xml set <url-pattern>/rest</url-pattern>. intensionally since according jax-rs document,

if both <servlet-mapping> , @applicationpath specified, <servlet-mapping> takes precedence.

i want test if case, no matter path use, http 404 error.

here example of way web.xml works:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">     <servlet>         <servlet-name>comingsoon</servlet-name>         <servlet-class>mysite.server.comingsoonservlet</servlet-class>     </servlet>     <servlet-mapping>         <servlet-name>comingsoon</servlet-name>         <url-pattern>/*</url-pattern>     </servlet-mapping> </web-app> 

maybe need have both servlet-name , servlet-class. notice servlet-name in servlet-mapping section has match servlet-name in servlet section.

notice how need star in url-pattern. yours "/rest/*".


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -