做门户网站经验,怎么自己做游戏软件,正规的网站制作电话多少,王展简历Spring-hateoas为应用程序创建遵循HATEOAS原理的基于REST的服务提供了一种极好的方法。 我的目的不是要展示如何创建服务本身#xff0c;而是要展示如何将客户端写入服务。 我将要使用的示例服务是Josh Long#xff08; starbuxman #xff09;编写的“ the-spring-rest-s… Spring-hateoas为应用程序创建遵循HATEOAS原理的基于REST的服务提供了一种极好的方法。 我的目的不是要展示如何创建服务本身而是要展示如何将客户端写入服务。 我将要使用的示例服务是Josh Long starbuxman 编写的“ the-spring-rest-stack ”。 我要使用的特定子项目是这里的阴影。 如果使用“ mvn jetty”命令运行此子项目则可以在http// localhost8080 / 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// localhost8080 / 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 ResourceT extends ResourceSupport {protected Resource() {this.content null;}public Resource(T content, Link... links) {this(content, Arrays.asList(links));}... 因此以上两种服务的使用者将获得以下两种类型 ResourceUser user .... //call to the serviceResourceCustomer customer ... //call to the service 现在的问题是由于上面的“用户”和“客户”是参数化类型如果我要使用杰克逊作为json处理器绑定这些类型我将按照以下方式进行操作 ObjectMapper objectMapper new ObjectMapper();
ResourceCustomer customer objectMapper.readValue(customerAsJson, Resource.class);
ResourceUser user objectMapper.readValue(userAsJson, Resource.class); 上面的方法不起作用原因是由于Java类型擦除导致参数化Resource的类型信息丢失Jackson不知道创建Resource User或Resource Customer的实例 解决方法是使用超类型令牌 这实质上是一种提供像杰克逊库类型的信息和我以前的博客上讲述它在这里 。 这样将json映射到适当的参数化类型的工作代码将如下所示 ObjectMapper objectMapper new ObjectMapper();
ResourceCustomer customer objectMapper.readValue(customerAsJson, new TypeReferenceResourceCustomer() {});
ResourceUser customer objectMapper.readValue(userAsJson, new TypeReferenceResourceUser() {}); Spring的基于Rest的服务的客户端抽象是RestTemplate 它可以使用称为HttpMessageConverter的抽象处理各种消息格式xmljsonatom等以处理每种消息格式的绑定细节。 Spring RestTemplate提供了自己的Super Type令牌实现能够按照Jackson的TypeReference的路线将不同的消息格式绑定到参数化类型这被称为ParameterizedTypeReference 。 ParameterizedTypeReference可用于通过以下方式将用户和客户的Rest响应完全绑定到Java类型 RestTemplate restTemplate new RestTemplate();
ResponseEntityResourceUser responseEntity restTemplate.exchange(http://localhost:8080/users/2, HttpMethod.GET, null, new ParameterizedTypeReferenceResourceUser() {}, Collections.emptyMap());
if (responseEntity.getStatusCode() HttpStatus.OK) {ResourceUser userResource responseEntity.getBody();User user userResource.getContent();
}RestTemplate restTemplate new RestTemplate();
ResponseEntityResourceCustomer responseEntity restTemplate.exchange(http://localhost:8080/users/2/customers/17, HttpMethod.GET, null, new ParameterizedTypeReferenceResourceCustomer() {}, Collections.emptyMap());
if (responseEntity.getStatusCode() HttpStatus.OK) {ResourceCustomer customerResource responseEntity.getBody();Customer customer customerResource.getContent();
} 总之ParameterizedTypeReference提供了一种处理参数化类型的巧妙方法并且在使用基于Spring Hateoas的REST服务时非常有用。 参考 all和其他博客中使用我们的JCG合作伙伴 Biju Kunjummen 提供的Spring RestTemplate和Super类型令牌使用Spring-hateoas Rest服务 。 翻译自: https://www.javacodegeeks.com/2014/01/consuming-spring-hateoas-rest-service-using-spring-resttemplate-and-super-type-tokens.html