欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Jersey Restful Application with tomcat

发布时间:2025/7/14 编程问答 49 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Jersey Restful Application with tomcat 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

##1、下载Jersey相关jar包,本示例用的版本是Jersey 2.22.1 ##2、eclipse新建dynamic web 项目,将Jersey相关包复制到WebContext/web-info/lib下,并添加项目对这些包的引用 ##3、编写入口类RestApplication

package com.newbsoft.restful;import org.glassfish.jersey.filter.LoggingFilter; import org.glassfish.jersey.server.ResourceConfig;public class RestApplication extends ResourceConfig {public RestApplication() {packages("com.newbsoft.restful");register(LoggingFilter.class);}}

##4、编写WebService 资源类(在Restful中,对客户端而已,所有操作请求都是申请资源)

package com.newbsoft.restful.spi;import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType;@Path("hello") public class HelloSpi {@GET@Produces(MediaType.TEXT_PLAIN)public String resHi(){return "hi~ it's me!";} }

##5、配置web.xml,启动Tomcat服务器

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><display-name>RestfulWS</display-name><servlet> <servlet-name>RestWS</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.newbsoft.restful.RestApplication</param-value></init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>RestWS</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list> </web-app>

##6、最后一步,测试 浏览器中输入http://localhost:8080/RestfulServer/rest/hello,即可看到页面返回 “hi~ it's me!” url说明:RestfulServer是项目部署目录名称

转载于:https://my.oschina.net/zwqlive/blog/526645

总结

以上是生活随笔为你收集整理的Jersey Restful Application with tomcat的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。