`

RESTful 中创建"资源"

 
阅读更多

RESTful 中所有的请求都是由资源来处理的。JAX-RS API实现的资源就是一个Java Class,这个Class被Annotated了

一个或多个Annotations; 使用JAX-RS实现的RESTful Web service是一个root resource class, 这个root resource class

 service expose后的访问的入口,该root resource class可以自己处理request,也可以由其sub-resource来处理request;

 即RESTfule 中有两种resource type: root resource class,sub-resource

 

1. Basic JAX-RS annotations

     (1) URI template syntax: URI template syntax

     (2) Specifying HTTP verbs

  •      javax.ws.rs.DELETE specifies that the method maps to a DELETE.
  •      javax.ws.rs.GET specifies that the method maps to a GET.
  •      javax.ws.rs.POST specifies that the method maps to a POST.
  •      javax.ws.rs.PUT specifies that the method maps to a PUT.
  •      javax.ws.rs.HEAD specifies that the method maps to a HEAD.

     (3) Root resource classes

  •      Class被用于Root Class需满足一下条件:
  •      Class 必须被标注@Path注解;
  •      Class必须有一个公共的构造函数用于运行期间调用;
  •      Class中的Method至少有一个被标注HTTP verb或@Path

     例如:

package demo.hw.server;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;

import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/customerservice/")
public class CustomerService {

	public CustomerService() {
	}

	@GET 
	public Customer getCustomer(@QueryParam("id") String id){
	}

	@DELETE
	public Response deleteCustomer(@QueryParam("id") String id){
	}

	@PUT
	public Response updateCustomer(Customer customer){
	}

	@POST
	public Response addCustomer(Customer customer){
	}

	@Path("/orders/{orderId}/") 
	public Order getOrder(@PathParam("orderId") String orderId){
	
	}
}

     (4) Resource Method

  •      所有的resource method需满足一下条件:
  •      必须是public;
  •      必须被标准HTTP Method 注解;
  •      不能有多有一个的实体参数。

     A. Resource Method的Paramter有两种类型:

          entity parameters;

          annotated parameters.

          例如:

@POST
@Path("disaster/monster/giant/{id}")
public void addDaikaiju(Kaiju kaiju,
@PathParam("id") String id){
}

    B. Resource Method的Return values

         void;

         any Java class for which the application has an entity provider;

         a Response object;

         a GenericEntity<T> object.

         所有的Resource Method返回一个HTTP Status Code给Request, 当返回值是void,null时,HTTP Status Code是

         200; 若返回值是除了null之外的其他值时,HTTP Status Code是204.

    (5) Sub-Resource

          有两种实现方式:

  •           Sub-Resource method: 对sub-resource直接使用HTTP verb;
  •           Sub-resource locator:只想一个实现了sub-resource的class.

          例如:Sub-resource methods :标注@Path注解和HTTP verb注解,sub-resource method直接响应处理request

          中指定的HTTP verb.      

@Path("/customerservice/")
public class CustomerService {

	@Path("/orders/{orderId}/")
	@GET
	public Order getOrder(@PathParam("orderId")
	String orderId) {
	}

	@Path("/orders/{orderId}/")
	@PUT
	public Order updateOrder(@PathParam("orderId")
	String orderId, Order order) {
	}

	@Path("/orders/")
	@POST
	public Order newOrder(Order order) {
	}
}

          例如:Sub-resource locators:没有HTTP verb注解,也不直接处理request; 而是sub-resource locator返回

           一个resource class instance来处理request,同时sub-resource不能有Entity Paramters.

@Path("/customerservice/")
public class CustomerService{
	
	@Path("/orders/{orderId}/")
	public Order processOrder(@PathParam("orderId") String orderId){
	}
}

public class Order{
	
	@GET
	public Order getOrder(@PathParam("orderId") String orderId){
	
	}
	
	@PUT
	public Order updateOrder(@PathParam("orderId") String orderId,Order order){
	
	}
}

     (7) Resource selection method

     JAX-RS选择resource method 算法被分成三个阶段:

     A. Determine the root resource class;

     B. Determine the object will handle the request;

     C. Select the resource method that will handle the request.

     1. Selecting from multiple resource classes

         当有多个resource匹配reqauest URI时,resource class优先于sub-resource;当有多个resource class时按照

         下面的条件匹配:

         (1) Prefer the resource with the most literal characters in its URI template;

         (2) Prefer the resource with the most variables in its URI template

         (3) Prefer the resource with the most variables containing regular expressions.

      2.Selecting from multiple resource methods

         按照下面的条件进行匹配:

         (1) Prefer resource methods over sub-resources;

         (2) Prefer sub-resource methods over sub-resource locaters;

         (3) Prefer methods that use the most specific values in the @Consumes annotation and the @Produces

         annotation; 例如:@Consumes(text/xml) > @Consumes(text/*) > @Consumes(*/*)

         (4) Prefer methods that most closely match the content type of the request body entity; HTTP Content-Type

         property.

         (5) Prefer methods that most closely match the content type accepted as a response; HTTP Accept property.

      3.Customizing the selection process

         实现org.apache.cxf.jaxrs.ext.ResourceComparator.

分享到:
评论

相关推荐

    Spring 3 来创建 RESTful

    RESTful Web Service 是一个使用 HTTP 和 REST 原理实现的 Web Service。通常,一个 RESTful Web Service 将定义基本资源 URI、它所支持的表示/响应 MIME,以及它所支持的操作。

    spring加载restful(文档+程序源码)

    通常,GET是用于获取或列出一个或多个资源,POST用于创建,PUT用于更新或替换,而DELETE则用于删除资源。  例如,GET http://host/context/employees/12345将获取ID为12345的员工的表示。这个响应表示可以是包含...

    用-Java-技术创建-RESTful-Web-服务

    Java servlets 常被用来开发 RESTful 应用程序。如何使用 servlet 并没有固定的模式。通常,servlet 会接受请求并自己解析这个 HTTP 请求 URI,以将此请求与一个已知资源相匹配。对于 REST 服务开发,这个简单的 ...

    RESTful API 设计最佳实践

    Roy Felding 在他论文 network based software architectures 的 第五章 中首次介绍了这些原则。 这些REST的关键原则与将你的 API 分割成逻辑资源紧密相关。使用HTTP请求控制这些资源,其中,这些方法(GET, POST, ...

    restful-resource:用于创建一致且RESTful资源请求JavaScript URL构建器,因此您不必这样做。 只有1.2kb

    用于创建一致且RESTful资源请求JavaScript URL构建器,因此您不必这样做。 安装 npm i @ hikerfeed / restful - resource -- save 用法 RESTful Resource是一个JavaScript URL构建器,用于创建一致的和RESTful资源...

    RestFul服务介绍

    1. REST介绍 REpresentational State Transfer (REST) 是一种架构原则,其中将 web 服务视为资源,可以由其 URL 唯一标识。...POST - 创建资源 GET - 检索资源 PUT – 更新资源 DELETE - 删除资源

    P23_同时创建父子资源_创建资源集合_Routine.Api2020_2_8.rar

    P23_同时创建父子资源_创建资源集合_Routine.Api2020_2_8.rar 使用 ASP.NET Core 杨旭 RESTful API ReSharper ApiController

    RESTful 教程.pdf

    本教程将指导你如何准备一个开发环境,开始使用 Jersey 框架工作,以创建基于 REST 的 Web 服务。 Jersey 框架实现 JAX-RS2.0 API ,这是建立标准规范 REST Web 服务。本教程也将教你如何设置 JDK , Tomcat 和 ...

    restful-markdown:在 Markdown 中构建 RESTful 文档的简单风格指南

    一个在 Markdown 中构建 RESTful 文档的简单风格指南,灵感来自 。 有很多方法可以为您的 RESTful API 创建文档。 但是,有时您只想将一些简单的内容与您的 git 存储库一起发布。 该项目旨在利用创建一个简单的 ...

    ng-srv:一个基于restful资源创建Angular.js模块的简单命令行工具

    ngSrv 文档一个基于restful资源创建Angular.js模块的简单命令行工具安装: $ npm install -g ng-srv用法: $ ng-srv [-OPTIONS] 选项: -h, --help output usage information-V, --version output the version ...

    restful:轻松创建 RESTful API 响应

    轻松创建 RESTful API 响应 此包使您能够创建 RESTful API 响应,而无需考虑底层 HTTP 层的细节。 您提供数据,包将处理输出格式和状态代码。 在应用程序错误的情况下,也可以创建 RESTful 错误响应。 该包针对与...

    P22_创建子资源POST_Routine.Api2020_2_8.rar

    P22_创建子资源POST_Routine.Api2020_2_8.rar 使用 ASP.NET Core 杨旭 RESTful API ReSharper ApiController P22 创建子资源POST 杨旭老师 ... ...Get请求信添加的数据的API地址 ...

    Crudify-Mongoose:用于创建 RESTful url 路由、呈现表单和处理给定 Mongoose 模型的创建、读取、更新和删除请求的简单生成器

    用于创建 RESTful url 路由、呈现表单和处理给定 Mongoose 模型的创建、读取、更新和删除请求的简单生成器 安装 $ npm install crudify-mongoose RESTful 路由 为提供的 Mongoose 模型生成 RESTful url 路由。 您...

    ZStack RESTful API

    本书将指导用户如何通过REST API来生成基于ZStack企业版的私有...第二章节为特定的私有云场景,主要描述一些现实环境中的私有云部署方案,并描述如何基于ZStack提供的基本资源,通过RESTful API完成私有云场景的创建。

    postman测试ES restful

    本资源主要用于测试ElasticSearch 的基本Restful API 操作,包括查询ES集群的状态,创建索引,给索引插入数据,删除索引,中文分词等。

    spring + cxf + restful + soap 集成小项目

    spring + cxf + restful + soap 方便初学者很快上手。 注解描述 @Path注解的值是一个相对的URI路径,这个路径指定了该Java类的位置,例如/helloworld。在这个URI中可以包含变量,例如可以获取用户的姓名然后作为参数...

    [WCF系列] 使用 .Net 3.5 技术创建 RESTful Web 服务 (英文版)

    [奥莱理] 使用 .Net 3.5 技术创建 RESTful Web 服务 [奥莱理] RESTful .NET Build and Consume RESTful Web Services with .NET 3.5 (E-Book) ☆ 出版信息:☆ [作者信息] Jon Flanders [出版机构] 奥莱理 ...

    RESTful Web Services 中文版.rar

    RESTful Web Services中文版 1,3,4章 缺第二章和其他章节,源码网无色会在第一时间补齐,敬请关注本页。 本身完整目录: 目录 序.......................................I. --------------------------...

    php-restful-mockery:在PHP中创建伪造的(模拟的)Restful API服务器以进行测试

    php-restful-mockery 在PHP中创建伪造的(嘲笑的)Restful API服务器以进行测试。安装将所有文件复制到运行目录。 该项目带有示例资源“用户”,可通过以下方式访问: (获取) 这将以JSON返回伪造数据。类图因为...

    P21_创建Company资源POST_P20_HTTP方法的安全性与幂等性_Routine.Api2020_2_7.rar

    创建Company资源POST 成功后Status:201 Created time:602ms Date:Fri, 07 Feb 2020 11:30:21 GMT Content-Type:application/json; charset=utf-8 Server:Kestrel Transfer-Encoding:chunked Location:...

Global site tag (gtag.js) - Google Analytics