使用jersey编码JAXRS服务并部署在tomcat上。应用子类
@ApplicationPath("/rest")
public class RestApplication extends Application {
public RestApplication() {
// TODO Auto-generated constructor stub
System.out.println("Inside RestApplication Constructor");
}
@Override
public Set<Class<?>> getClasses() {
// TODO Auto-generated method stub
System.out.println("Get Class");
Set<Class<?>> s=new HashSet<Class<?>>();
s.add(SupportDataService.class);
return s;
}
}
资源类
@Path("/supportdata")
public class SupportDataService {
public SupportDataService() {
// TODO Auto-generated constructor stub
System.out.println("Inside SupportDataService Constructor");
}
@Path("/support")
@GET
@Produces(MediaType.TEXT_XML)
public String getSupportData(){
String xmlSupport=null;
xmlSupport="<SupportData><Support><key>path1</key><value>value1</value></Support><Support><key>path2</key><value>value2</value></Support></SupportData>";
return xmlSupport;
}
}
在WEB-INF / lib中添加了除javax.servlet-api-3.0.1.jar之外的所有jersey jar并点击url
http://localhost:8080/RestConfigurator/rest/supportdata/support
但得到404错误。未指定任何web.xml作为子类Application。
答案 0 :(得分:0)
因此,对于Tomcat 6,这是一个Servlet 2.5实现,我们需要有一个web.xml声明ServletContainer
。您可以查看有关它的更多信息here。基本上,如果你想保留RestApplication
课程,web.xml
会看起来像
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>thepackge.of.RestApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
鉴于你已经添加了来自Jersey JAX-RS 2.0 RI bundle的所有jar(除了servlet-api),这应该可行。我已经在Tomcat 6上使用Maven进行了测试,它运行正常。 Maven只是从那个捆绑中拉出了所有大部分的罐子。但是应用程序部署并没有区别。