jersey rest webservice
参考官网:https://jersey.github.io/documentation/latest/getting-started.html#new-webapp
创建一个 JavaEE Web Application
命令:mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.example -DartifactId=simple_service_webapp -Dpackage=com.example -DarchetypeVersion=2.26
如下图:
这样就建立一个simple_service_webapp的web程序,查看simple_service_webapp的结构:
cd simple_service_webapp
tree /f
运行程序后生产target文件夹
web.xml文件
<servlet> <servlet-name>Jersey Web Application</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.example</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey Web Application</servlet-name> <url-pattern>/webapi/*</url-pattern> </servlet-mapping>
代码文件
package com.example; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; /** * Root resource (exposed at "myresource" path) */ @Path("myresource") public class MyResource { /** * Method handling HTTP GET requests. The returned object will be sent * to the client as "text/plain" media type. * * @return String that will be returned as a text/plain response. */ @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return "Got it!"; } }
pom.xml文件
生成war包并部署到tomcat下
生成war包,必须选择
运行编译生成 simple_service_webapp.war
把simple_service_webapp.war文件拷贝到tomcat的webapps下面
启动apache-tomcat-9.0.0.M26\bin下面的startup.bat就可以成功部署,这样会在webapps下面生成文件夹simple_service_webapp
文件夹结构如下: