这是我的项目文件夹
http://postimg.org/image/huftiysmn/
这是 Hello.java 代码:
package ale;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/hello")
public class Hello {
@GET
public String hello(){
return "hello world";
}
}
和 web.xml 文件:
<?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>
当我在 URL 上从 eclipse 启动 tomcat 时:localhost:8080/Ciao/rest/hello,我得到“HTTP Status 404 - Not Found”
我从来没有写过休息应用程序,所以它可能是愚蠢的,但我不知道哪里出了问题,有什么想法吗?
最佳答案
你需要创建@XmlRootElement
尝试这样的事情
类hi.java
@XmlRootElement
public class hello {
private String hi;
public String getHi() {
return hi;
}
public void setHi(String hi) {
this.hi = hi;
}
}
类 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;
}
}
关于java - 找不到简单 Jersey Hello World 404 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25272664/