新的Jersey版本,支持Servlet 3.x,与Servlet 2.x不一样
实现自定义的Application,使用@ApplicationPath("/")标注
@ApplicationPath("/")
public class MyApplication extends ResourceConfig{
public SportApplication() {
super(A.class,B.class,C.class); // *添加,控制,也可以用packages
}
}
使用web.xml,新版本可以不用web.xml了,但是有局限性,推荐使用web.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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Servlet declaration can be omitted in which case
it would be automatically added by Jersey -->
<servlet>
<servlet-name>package.MyApplication</servlet-name>
</servlet>
<!-- Servlet mapping can be omitted in case the Application subclass
is annotated with @ApplicationPath annotation; in such case
the mapping would be automatically added by Jersey -->
<servlet-mapping>
<servlet-name>package.MyApplication</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
导出war包至tomcat的webapps目录下,直接网站访问即可
导出war包要求项目必须是web项目,如果不是web项目,可以右键项目 -> Configure -> Convert to faceted.. 勾选Dynamic Web Project即可
如果项目没有web.xml文件,在项目转换成web项目之后,可以右键项目 -> Java EE Tools -> Generate Depolyment Descriptor Stub
导出war包之前,确认lib库要相应的一同导出,项目属性 -> Deployment Assembly -> Add -> Java Build Path -> 勾选maven Jars或者其他依赖库
导出war包,右键项目 -> Export to War
这样的配置在部署之后,就可以通过链接 http://xxx.com/mywar/rest/...访问resource了