这是我的项目文件夹

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>


当我从eclipse的URL:localhost:8080 / Ciao / rest / hello启动tomcat时,我收到了“ HTTP Status 404-Not Found”

我从没写过rest应用程序,所以可能有些愚蠢,但我不知道出什么问题了,有什么主意吗?

最佳答案

您需要创建@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;
    }
}

07-24 09:46
查看更多