Fag*_*ack 3 java rest jboss jax-rs jakarta-ee
我正在尝试使用一个简单的 JAX RS 示例,但我没有这样做。
网页.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>PLAYGROUND</display-name>
<servlet-mapping>
<servlet-name>playground.Rest</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
REST.java
package playground;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
public class Rest extends Application {
@GET
@Path("hello")
public String helloworld() {
return "Hello World!";
}
}
Run Code Online (Sandbox Code Playgroud)
http://localhost/{warcontext}/api/hello使用浏览器 (GET)访问给我 404 错误状态
这可能是非常愚蠢的事情,但我无法弄清楚。
使用:JBoss EAP 6.1.0 (Java EE 6)
您需要扩展javax.ws.rs.core.Application(它可以保持为空)并使用@ApplicationPath("/ide") 对其进行注释,然后创建一个JAX-RS 资源,即一个带有@Path(" /hello") 注释。在本课程中,您只需要使用 @GET 注释您的 JAX-RS 资源方法。
@ApplicationPath("/ide")
public class Rest extends Application { }
@Path("/hello")
public class HelloResource {
@GET
@Path("hello")
public String helloworld() {
return "Hello World!";
}
}
Run Code Online (Sandbox Code Playgroud)
你也可以看看这个例子:https : //github.com/resteasy/Resteasy/tree/master/jaxrs/examples/oreilly-workbook/ex03_1
网页.xml
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>javax.ws.rs.core.Application</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
REST.java
public class Rest {
@GET
@Path("hello")
public String helloworld() {
return "Hello World!";
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要,您可以添加
你的应用程序
package playground;
import javax.ws.rs.core.Application;
...
public class YourApplication extends Application {
@Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> yourResources = new HashSet<Class<?>>();
yourResources.add(InvoiceResource.class);
return yourResources;
}
}
Run Code Online (Sandbox Code Playgroud)
然后
网页.xml
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>playground.YourApplication</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
如果添加@ApplicationPath("/api")for YouApplication,则 web.xml 不应包含servlet-mapping。