基于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服务器请求资源

这里写图片描述

这里写图片描述

参考链接:

  1. 使用Jersey和Apache Tomcat搭建构建Restful Web服务
    https://www.ibm.com/developerworks/cn/web/wa-aj-tomcat/
  2. 最好的8个Java Restful框架
    http://colobu.com/2015/11/15/best-available-java-restful-micro-frameworks/
  3. Java Restful框架的性能比较
    http://colobu.com/2015/11/17/Jax-RS-Performance-Comparison/
  4. Jersey User Guide
    https://jersey.github.io/documentation/latest/index.html
  5. Maven-Jersey的库
    https://mvnrepository.com/search?q=jersey
  6. Jar包下载地址
    http://www.java2s.com/Code/Jar/CatalogJar.htm
  7. package javax.servlet does not exist错误解决方案:
    https://stackoverflow.com/questions/16262948/error-package-javax-servlet-does-not-exist
  8. 通过Jersey客户端API调用RestFul风格的Web服务
    http://blog.csdn.net/qq_31382921/article/details/52396881
  9. 通过Jersey快速实现rest风格的webservice
    http://blog.csdn.net/passion_wu128/article/details/51393775
  10. webservice框架jersey简单总结
    http://dyygusi.iteye.com/blog/2148029
  11. Jersey:你值得拥有的Java Restful框架
    http://hao.jobbole.com/jersey/
  12. 在Dubbo中开发REST风格的远程调用(RESTful remoting)
    https://dangdangdotcom.github.io/dubbox/rest.html
阅读全文
0 0
原创粉丝点击