简体   繁体   中英

Simple Jersey Hello World 404 resource not found

This is my project folder

http://postimg.org/image/huftiysmn/

here is the Hello.java code:

package ale;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/hello")
public class Hello {

    @GET
    public String hello(){
        return "hello world";
    }
}

and the web.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1"
 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
 <display-name>Ciao</display-name>
 <servlet>
  <display-name>Rest Servlet</display-name>
  <servlet-name>RestServlet</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
   <param-name>com.sun.jersey.config.property.packages</param-name>
   <param-value>ale</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>RestServlet</servlet-name>
  <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>

</web-app>

when i launch tomcat from eclipse at the URL: localhost:8080/Ciao/rest/hello, i got "HTTP Status 404 - Not Found"

i never wrote a rest application so it might be something stupid but i cant figure out what is wrong, any idea?

You need create @XmlRootElement

try something like this

Class hi.java

@XmlRootElement
public class hello {

    private String hi;

    public String getHi() {
        return hi;
    }
    public void setHi(String hi) {
        this.hi = hi;
    }

}

Class ale.java

package ale;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/hello")
public class Hello {

    @GET
    public String hello(){
        return getHi;
    }
}

Im kinda late but here is the solution i found:i actually solved the problem converting my project to a maven project. i think the problem was in some version mismatch between some jars in my library. @keyser after the library fix the project is working either with or without the slash in the path

像这样将pom.xml 中的 jersey 依赖项的版本更改为 1.19.4 文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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