基于Jersey框架搭建Restful Java Web Service的基本步骤
来源:互联网 发布:linux caja 编辑:程序博客网 时间:2024/05/21 07:14
本文由Markdown语法编辑器编辑完成。
1. Restful Web Service
2. Restful的基本框架
3. 基于Jersey框架搭建Restful Web Service的基本步骤
Jersey框架不仅实现了JAX-RS规范,还提供了自有API以扩展JAX-RS, 提供了更多的特性和工具进一步简化Restful Web Service及其客户端开发。
一个典型的Jersey RESTful Web Services就是一个简单的java类+Jersey提供的注解,例如:
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!"; }}
@Path(“myresource”)表示MyResource类处理路径为/myresource的请求,@GET表示请求是GET请求,@Produces(MediaType.TEXT_PLAIN)表示服务返回的数据类型是简单文本类型。
按照上面的写法如果把RESTful的服务搭建在本地的Tomcat服务器上的话,那么向该服务发送Get请求的时候的URL应该为:
localhost:8080/myresource。
3.1 项目源码载入Eclipse:
从链接https://www.ibm.com/developerworks/cn/web/wa-aj-tomcat/下载项目的源码,并且载入Eclipse工程中,可以看到项目的组织结构如下图所示:
(图中红色线框包含的lib,都是根据相关类的需求,从jar下载网站下载后载入进工程的lib库中的)
项目的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Jersey</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>sample.hello.resources</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping></web-app>
3.2 多种客户端向Restful服务请求资源实例:
将该Java web项目载入Tomcat 8.0中,启动服务后,即可根据以下的访问地址,向该Restful Web Service发起请求。
发起请求可以有多种方式发起:
(1)直接在Chrome等浏览器的地址栏中输入Restful资源的URL,即可默认向Restful服务器发起Get请求;
(2)用SoapUI或Google的插件Postman,可以模拟由客户端向Restful服务器发起Get, Put, Post, Delete等请求。
3.2.1 Chrome浏览器请求Restful资源
以下是在Chrome浏览器中通过直接输入Resources的URL后,返回的Json资源:
输入地址为:localhost:8080/ProjectName/rest/hello
ProjectName = “Jersey.Sample.Contact.Src”.
输入地址为: localhost:8080/ProjectName/rest/contacts
3.2.2 SoapUI向Restful服务器请求资源
参考链接:
- 使用Jersey和Apache Tomcat搭建构建Restful Web服务
https://www.ibm.com/developerworks/cn/web/wa-aj-tomcat/ - 最好的8个Java Restful框架
http://colobu.com/2015/11/15/best-available-java-restful-micro-frameworks/ - Java Restful框架的性能比较
http://colobu.com/2015/11/17/Jax-RS-Performance-Comparison/ - Jersey User Guide
https://jersey.github.io/documentation/latest/index.html - Maven-Jersey的库
https://mvnrepository.com/search?q=jersey - Jar包下载地址
http://www.java2s.com/Code/Jar/CatalogJar.htm - package javax.servlet does not exist错误解决方案:
https://stackoverflow.com/questions/16262948/error-package-javax-servlet-does-not-exist - 通过Jersey客户端API调用RestFul风格的Web服务
http://blog.csdn.net/qq_31382921/article/details/52396881 - 通过Jersey快速实现rest风格的webservice
http://blog.csdn.net/passion_wu128/article/details/51393775 - webservice框架jersey简单总结
http://dyygusi.iteye.com/blog/2148029 - Jersey:你值得拥有的Java Restful框架
http://hao.jobbole.com/jersey/ - 在Dubbo中开发REST风格的远程调用(RESTful remoting)
https://dangdangdotcom.github.io/dubbox/rest.html
- 基于Jersey框架搭建Restful Java Web Service的基本步骤
- jersey (RESTful Web Service框架)
- 基于RESTful Web Service 的Jersey框架与Spring完美整合
- RESTful Web Service框架jersey简单总结
- jersey 基于tomcat web service 开发 restful service
- 基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
- 基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
- 基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
- 基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
- 基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
- Jax-rs\Jersey框架与RESTful Web Service
- Jax-rs\Jersey框架与RESTful Web Service
- Java Web Service-CXF -基于Restful的Web服务
- Jersey -RESTful Web Services in Java基础框架示例
- 基于jersey搭建RESTful风格的WebService接口
- ubuntu 下面通过maven搭建restful风格的jersey框架
- 用Java实现RESTful Web Services框架的简要步骤
- 使用 Jersey + Tomcat 輕鬆開發 RESTful Web Service
- 使用samba让windows同linux文件共享
- Windows下 Maven 使用 阿里云镜像配置
- Webservice开发全程技术
- MySQL系列—索引相关的数据结构和算法(B+Tree)
- [模板练习]对拍
- 基于Jersey框架搭建Restful Java Web Service的基本步骤
- array_push()方法
- uva-1347经典dp,边界设置很重要
- Q136:PBRT-V3,双向路径追踪(Bidirectional Path Tracing)(16.3章节)
- 8.12 python相关环境以及套件安装
- xshell连接不到虚拟机。
- oracleday12
- 权利的游戏-三眼乌鸦喝水算法Aesop's Fables
- 文本挖掘--DNN和LSTM的比较