使用Spring RestTemplate和Super类型令牌消费Spring-hateoas Rest服务
Spring-hateoas为应用程序创建遵循HATEOAS原理的基于REST的服务提供了一种极好的方法。
我的目的不是要展示如何创建服务本身,而是要展示如何将客户端写入服务。
我将要使用的示例服务是Josh Long( @starbuxman )编写的“ the-spring-rest-stack ”。 我要使用的特定子项目是这里的阴影。 如果使用“ mvn jetty”命令运行此子项目,则可以在http:// localhost:8080 / users / 2中使用基于REST的端点列出用户的详细信息,其中“ 2”是用户的ID,并给出一个以下结构的结果:
{"links": [{"rel": "self","href": "http://localhost:8080/users/2"}, {"rel": "customers","href": "http://localhost:8080/users/2/customers"}, {"rel": "photo","href": "http://localhost:8080/users/2/photo"}],"id": 2,"firstName": "Lois","profilePhotoMediaType": null,"lastName": "Lane","username": "loislane","password": null,"profilePhotoImported": false,"enabled": true,"signupDate": 1370201631000
}
为了获得该用户的特定客户,端点位于http:// localhost:8080 / users / 2 / customers / 17,它提供以下结构的输出:
{"links": [{"rel": "self","href": "http://localhost:8080/users/2/customers/17"}, {"rel": "user","href": "http://localhost:8080/users/2"}],"id": 17,"signupDate": 1372461079000,"firstName": "Scott","lastName": "Andrews","databaseId": 17
}
现在,对于这两种服务的使用者而言,结果可以由Spring-hateoas项目中的一个称为Resource的Java类型表示,并且是具有以下签名的泛型类:
public class Resource<T> extends ResourceSupport {protected Resource() {this.content = null;}public Resource(T content, Link... links) {this(content, Arrays.asList(links));}...
因此,以上两种服务的使用者将获得以下两种类型:
Resource<User> user = .... //call to the serviceResource<Customer> customer = ... //call to the service
现在的问题是,由于上面的“用户”和“客户”是参数化类型,如果我要使用杰克逊作为json处理器绑定这些类型,我将按照以下方式进行操作:
ObjectMapper objectMapper = new ObjectMapper();
Resource<Customer> customer = objectMapper.readValue(customerAsJson, Resource.class);
Resource<User> user = objectMapper.readValue(userAsJson, Resource.class);
上面的方法不起作用,原因是由于Java类型擦除导致参数化Resource的类型信息丢失,Jackson不知道创建Resource <User>或Resource <Customer>的实例
解决方法是使用超类型令牌 ,这实质上是一种提供像杰克逊库类型的信息和我以前的博客上讲述它在这里 。 这样,将json映射到适当的参数化类型的工作代码将如下所示:
ObjectMapper objectMapper = new ObjectMapper();
Resource<Customer> customer = objectMapper.readValue(customerAsJson, new TypeReference<Resource<Customer>>() {});
Resource<User> customer = objectMapper.readValue(userAsJson, new TypeReference<Resource<User>>() {});
Spring的基于Rest的服务的客户端抽象是RestTemplate ,它可以使用称为HttpMessageConverter的抽象处理各种消息格式(xml,json,atom等),以处理每种消息格式的绑定细节。
Spring RestTemplate提供了自己的Super Type令牌实现,能够按照Jackson的TypeReference的路线将不同的消息格式绑定到参数化类型,这被称为ParameterizedTypeReference 。
ParameterizedTypeReference可用于通过以下方式将用户和客户的Rest响应完全绑定到Java类型:
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Resource<User>> responseEntity =restTemplate.exchange("http://localhost:8080/users/2", HttpMethod.GET, null, new ParameterizedTypeReference<Resource<User>>() {}, Collections.emptyMap());
if (responseEntity.getStatusCode() == HttpStatus.OK) {Resource<User> userResource = responseEntity.getBody();User user = userResource.getContent();
}
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Resource<Customer>> responseEntity =restTemplate.exchange("http://localhost:8080/users/2/customers/17", HttpMethod.GET, null, new ParameterizedTypeReference<Resource<Customer>>() {}, Collections.emptyMap());
if (responseEntity.getStatusCode() == HttpStatus.OK) {Resource<Customer> customerResource = responseEntity.getBody();Customer customer = customerResource.getContent();
}
总之,ParameterizedTypeReference提供了一种处理参数化类型的巧妙方法,并且在使用基于Spring Hateoas的REST服务时非常有用。
翻译自: https://www.javacodegeeks.com/2014/01/consuming-spring-hateoas-rest-service-using-spring-resttemplate-and-super-type-tokens.html
使用Spring RestTemplate和Super类型令牌消费Spring-hateoas Rest服务相关推荐
- 使用Jackson和Super类型令牌进行Json反序列化
Datatables是一个jquery插件,用于显示表格信息–它可以增强简单的表或可以使用基于AJAX的数据并以表格形式显示信息. 数据表要求来自服务器的数据遵循特定的JSON格式才能在屏幕上显示 ...
- 反序列化 jackson_使用Jackson和Super类型令牌的Json反序列化
反序列化 jackson Datatables是一个jquery插件,用于显示表格信息–它可以增强简单的表或可以使用基于AJAX的数据并以表格形式显示信息. 数据表要求来自服务器的数据遵循特定的J ...
- Spring RestTemplate示例
Spring RestTemplate示例 Spring RestTemplate提供了一种测试RESTful Web服务的便捷方式. 目录[ 隐藏 ] 1 Spring RestTemplate 1 ...
- Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求
Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939 版权声明:本 ...
- 具有链接资源的Spring RestTemplate
Spring Data REST是一个了不起的项目,它提供了一些机制来将基于Spring Data的存储库中的资源公开为REST资源. 使用链接资源公开服务 考虑两个简单的基于JPA的实体,课程和教师 ...
- spring AbstractBeanDefinition创建bean类型是动态代理类的方式
1.接口 Class<?> resourceClass 2.获取builder BeanDefinitionBuilder builder = BeanDefinitionBuilder. ...
- java rest post list_java – 如何使用Spring RestTemplate在POST中传递数组?
你可以查看这篇文章: How to pass List or String array to getForObject with Spring RestTemplate,该帖子的解决方案是: 列表或其 ...
- Spring RestTemplate中文乱码解决方案
Spring RestTemplate中文乱码解决方案 参考文章: (1)Spring RestTemplate中文乱码解决方案 (2)https://www.cnblogs.com/accesski ...
- java 获取400的错误信息_获取400错误的请求Spring RestTemplate POST
我想使用POST方法使用Spring Rest web服务.我无法在客户端(网站)访问POJO文件,因此必须在客户端使用JSON并在Web服务中使用POJO.下面是我的代码:获取400错误的请求Spr ...
最新文章
- php 伪静态 page-18.html,PHP 伪静态实现技术原理讲解
- windows 系统常用操作
- 牛!这位斯坦福PhD新生的论文被引数:接近4万
- 翻译:Single Sign-On for Everyone
- 云炬60s看世界20211117
- TCP/IP 7.2 OSPF 虚链路
- 六十、走进位运算的大门
- 正态分布图_用EXCEL简易制作正态分布图
- 案例逐步演示python利用正则表达式提取指定内容并输出到csv
- 利用SVD-推荐未尝过的菜肴2
- 2017.10.28 排序 思考记录
- openwrt拦截snmp报文
- 【PBRT】圆盘均匀采样,python实现
- 说课稿模板计算机,计算机说课稿
- TOPOLOGY ADAPTIVE GRAPH CONVOLUTIONAL NETWORKS论文笔记(TAGConv)
- asp.net 中 使用ajax 和浏览器的关系
- 511遇见易语言计次循环首九九乘法表
- 数字中国理念引领国企人力资源数字化转型与实践
- zbbz的lisp_Github上四种Lisp方言的流行度
- 联想开天s620z改win7和Linux,联想ThinkPad笔记本win10改win7系统及BIOS设置图文教程
热门文章
- HBase出现java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguration问题
- android 画布控件,Android canvas画图操作之切割画布实现方法(clipRect)
- 常数除以0的极限是什么_【极限】第四节 极限运算法则
- mybatis的$和#详解分析
- ue4 运行禁用鼠标_[UE4] VS code使用LuaPanda断点调试
- 使用log4j2打印mybatis的sql执行日志
- 防火墙例外里没有远程桌面_证明没有例外
- 漫反射 高光反射_如何有效地使用反射
- 使用AWS Lambda,S3和AWS CloudFront进行动态内容缓存
- Java应用程序中的验证