这些是我在Eclipse IDE for Java EE中使用Jax-RS创建简单的RESTful Web服务时遵循的步骤。>>
my.rest.example
),并在该包下创建一个类(RestService
)javax.ws.rs-api-2.0.jar
并将其添加到构建路径中,以解决javax.ws.rs-api-2.0.jar
导入错误javax.ws.rs
RestService.java
package my.rest.example;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
@Path("/MyRestService")
@ApplicationPath("/resources")
public class RestService extends Application {
// http://localhost:8080/RestExample/resources/MyRestService/sayHello
@GET
@Path("/sayHello")
public String getHelloMsg() {
return "Hello World";
}
}
web.xml
在浏览器中打开此URL: <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>RestExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
返回此:
http://localhost:8080/RestExample/resources/MyRestService/sayHello
控制台输出
HTTP ERROR 404 Not Found URI: /RestExample/resources/MyRestService/sayHello STATUS: 404 MESSAGE: Not Found SERVLET: default
预期输出为
Starting preview server on port 8080 Modules: RestExample (/RestExample) 2020-05-21 11:45:45.175:INFO::main: Logging initialized @1815ms to org.eclipse.jetty.util.log.StdErrLog 2020-05-21 11:45:45.894:INFO:oejs.Server:main: jetty-9.4.27.v20200227; built: 2020-03-02T14:40:42.212Z; git: a304fd9f351f337e7c0e2a7c28878dd536149c6c; jvm 1.8.0_171-b11 2020-05-21 11:45:48.219:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /RestExample, did not find org.eclipse.jetty.jsp.JettyJspServlet 2020-05-21 11:45:48.289:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0 2020-05-21 11:45:48.289:INFO:oejs.session:main: No SessionScavenger set, using defaults 2020-05-21 11:45:48.299:INFO:oejs.session:main: node0 Scavenging every 600000ms 2020-05-21 11:45:48.425:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@7d907bac{RestExample,/RestExample,file:///C:/.../wip/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/RestExample/,AVAILABLE}{C:/.../wip/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/RestExample} 2020-05-21 11:45:48.489:INFO:oejs.AbstractConnector:main: Started ServerConnector@6ed3ef1{HTTP/1.1, (http/1.1)}{0.0.0.0:8080} 2020-05-21 11:45:48.504:INFO:oejs.Server:main: Started @5150ms
。我在这里想念什么?
以下步骤基于Youtube上的教程:Hello World
。
Eclipse版本
:2020-03(4.15.0)这些是我在Eclipse IDE for Java EE中使用Jax-RS创建简单的RESTful Web服务的步骤。创建一个新的Dynamic Web Project(名称:RestExample)选择目标运行时为J2EE ...
您正在使用URL中的显示名称。但是根据文档: