我正在尝试使用 RestEasy 创建示例 REST 服务。下面是我的Java类
@Path("/message")
public class MessageRestService {
@GET
@Path("/{param}")
public Response getMessage(@PathParam("param")String msg){
String result = "Hello World "+msg;
return Response.status(200).entity(result).build();
}
}
我的
web.xml
如下
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<!-- this need same with resteasy servlet url-pattern -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.html
</welcome-file>
</welcome-file-list>
</web-app>
我使用的RestEasy版本是3.0.12。当我点击如下网址时
http://localhost:8080/RestPathAnnotationExample/rest/message/test
找不到此
localhost
页面。
No webpage was found for the web address: http://localhost:8080/RestPathAnnotationExample/rest/message/test
我正在 Tomcat 8 上部署。
您同时指定了
resteasy.servlet.mapping.prefix=/rest
和
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
这将在指定路径上使用映射。除非您提供正确的配置,否则它应该可以工作。您可以了解更多有关安装和配置的信息。
RESTeasy 默认配置为扫描这些目录中的 jar 和类以查找 JAX-RS 带注释的类,并在系统中部署和注册它们。