原文地址:http://www.javaarch.net/jiagoushi/698.htm
Spring rest对etag支持 etag(entity tag)是http响应头中用来判断对应响应结果是否修改。可以使用hash算法来计算etag的值。 比如:第一次访问 curl -H "Accept: application/json" -i http://localhost:8080/rest-sec/api/resources/1 响应为: HTTP/1.1 200 OK ETag: "f88dd058fe004909615a64f01be66a7" Content-Type: application/json;charset=UTF-8 Content-Length: 52 那么下次客户端访问的时候If-None-Match或者If-Match头来让服务端判断是否修改 curl -H "Accept: application/json" -H 'If-None-Match: "f88dd058fe004909615a64f01be66a7"' -i http://localhost:8080/rest-sec/api/resources/1 如果没有修改,则服务端会返回 HTTP/1.1 304 Not Modified ETag: "f88dd058fe004909615a64f01be66a7" 我们重新修改请求参数: curl --user admin@fake.com:adminpass -H "Content-Type: application/json" -i -X PUT --data '{ "id":1, "name":"newRoleName2", "description":"theNewDescription" }' http://localhost:8080/rest-sec/api/resources/1 响应为: HTTP/1.1 200 OK ETag: "d41d8cd98f00b204e9800998ecf8427e" Content-Length: 0 如果我们再访问 curl -H "Accept: application/json" -H 'If-None-Match: "f88dd058fe004909615a64f01be66a7"' -i http://localhost:8080/rest-sec/api/resources/1 则返回: HTTP/1.1 200 OK ETag: "03cb37ca667706c68c0aad4cb04c3a211" Content-Type: application/json;charset=UTF-8 Content-Length: 56 spring对etag的支持,在web.xml中加上这个filter <filter> <filter-name>etagFilter</filter-name> <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class> </filter> <filter-mapping> <filter-name>etagFilter</filter-name> <url-pattern>/api/*</url-pattern> </filter-mapping> 测试etags 第一次判断etag不为空 @Test public void givenResourceExists_whenRetrievingResource_thenEtagIsAlsoReturned() { // Given Resource existingResource = getApi().create(new Resource()); String uriOfResource = baseUri + "/" + existingResource.getId(); // When Response findOneResponse = RestAssured.given(). header("Accept", "application/json").get(uriOfResource); // Then assertNotNull(findOneResponse.getHeader(HttpHeaders.ETAG)); } 第二次,我们从上一次取出etag之后把etag发送给服务端验证 @Test public void givenResourceWasRetrieved_whenRetrievingAgainWithEtag_thenNotModifiedReturned() { // Given T existingResource = getApi().create(createNewEntity()); String uriOfResource = baseUri + "/" + existingResource.getId(); Response findOneResponse = RestAssured.given(). header("Accept", "application/json").get(uriOfResource); String etagValue = findOneResponse.getHeader(HttpHeaders.ETAG); // When Response secondFindOneResponse= RestAssured.given(). header("Accept", "application/json").headers("If-None-Match", etagValue) .get(uriOfResource); // Then assertTrue(secondFindOneResponse.getStatusCode() == 304); } 测试服务端响应结果变化了,则 @Test public void givenResourceWasRetrieved_whenRetrievingAgainWithEtag_thenNotModifiedReturned() { // Given T existingResource = getApi().create(createNewEntity()); String uriOfResource = baseUri + "/" + existingResource.getId(); Response findOneResponse = RestAssured.given(). header("Accept", "application/json").get(uriOfResource); String etagValue = findOneResponse.getHeader(HttpHeaders.ETAG); existingResource.setName(randomAlphabetic(6)) getApi().update(existingResource.setName(randomString)); // When Response secondFindOneResponse= RestAssured.given(). header("Accept", "application/json").headers("If-None-Match", etagValue) .get(uriOfResource); // Then assertTrue(secondFindOneResponse.getStatusCode() == 200); } 如果请求带上不正确的etag值并且加上if-match判断,则会返回412Precondition Failed @Test public void givenResourceExists_whenRetrievedWithIfMatchIncorrectEtag_then412IsReceived() { // Given T existingResource = getApi().create(createNewEntity()); // When String uriOfResource = baseUri + "/" + existingResource.getId(); Response findOneResponse = RestAssured.given().header("Accept", "application/json"). headers("If-Match", randomAlphabetic(8)).get(uriOfResource); // Then assertTrue(findOneResponse.getStatusCode() == 412); } github示例地址:https://github.com/eugenp/REST
相关推荐
Spring REST Docs。 官网 Spring REST Docs API。 Spring REST Docs 开发文档。
spring rest 增删改查(put\delete\post\get)简单示例详细讲解,亲测可用。spring rest 增删改查(put\delete\post\get)简单示例详细讲解,亲测可用。spring rest 增删改查(put\delete\post\get)简单示例详细...
Spring REST is a practical guide for designing and developing RESTful APIs using the Spring Framework. This book walks you through the process of designing and building a REST application while taking...
Spring3.0 rest 实例,实现rest风格的访问
spring rest doc 例子,根据里面的例子,配合你的项目我相信能很快就用的上哦。尝试着用用吧。 spring rest doc 例子,根据里面的例子,配合你的项目我相信能很快就用的上哦。尝试着用用吧。
Spring MVC REST Demo
在版本1.2.4中,我们还添加了对支持, 是用于JSON API的轻型Angular2 +适配器 特征 从模型类中生成TypeScript接口,并从REST控制器中生成基于Observable API的完整的Angular服务或基于Promises API的纯JavaScript...
介绍restlet如何整合spring框架进行开发。Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的,广泛的应用于应用项目中。 具体教程可以查看http://www.lifeba.org/arch/restlet_spring_3.html。
使用Spring MVC创建REST服务简单例子
spring boot 连接数据库的例子,可以调试运行,目前流行的开发模式。
Spring REST
基于spring mina 封装 rest 形式接口服务器,摆脱对tomcat,resin等服务器的依赖,基于spring,mina本身可提供tcp/ip接口,同时封装rest可方面提供http形式rest接口访问服务,方便接入
spring mvc基础开发 最基础类 一目了然 适合初学者
rest+spring+hibernate rest和spring之间用json交互
◆全面的REST支持:在Spring MVC中天然支持REST,如REST式的请求映射,URI变量提取等。 ◆富Portlet 2.0支持:Spring MVC完全支持Portlet 2.0环境以及Portlet 2.0中新的事件与资源请求模型。 ◆对象/XML映射(OXM...
自己找了资料整合了一份 jersey+spring rest webservice web maven服务器架构,希望有需要的人可以下载使用;
NULL 博文链接:https://leiwuluan.iteye.com/blog/1539767
这是一个基于maven构建的java项目,整合了Apache CXF框架,实现restful风格的service
这些内容是作者个人总结所的,主要描述了如何使用Spring MVC搭建Rest服务的全过程,并提供了整个搭建过程的源码。整个搭建过程中使用了Jetty、JAXB2.0和Spring等相关框架和技术,希望下载的人能够对这些技术有所了解...
Building a Rest API with Spring Building a Rest API with Spring