《Spring in Action》第7章-使用REST服务

本文深入探讨了Spring框架中的RestTemplate与Traverson工具类的使用方法,包括资源的获取、替换、删除和新建等操作。RestTemplate提供了一系列方便调用REST服务的方法,而Traverson则用于处理HATEOAS风格的API,两者结合可以实现更强大的REST API交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用REST服务

RestTemplate

同JDBCTemplate一样,为了消除大量繁琐的样板代码,RestTemplate提供了很多方法,使得我们能够很方便的调用REST服务。这些方法大致可分为以下12组重载方法:

Method groupDescription

getForObject

Retrieves a representation via GET.

getForEntity

Retrieves a ResponseEntity (that is, status, headers, and body) by using GET.

headForHeaders

Retrieves all headers for a resource by using HEAD.

postForLocation

Creates a new resource by using POST and returns the Location header from the response.

postForObject

Creates a new resource by using POST and returns the representation from the response.

postForEntity

Creates a new resource by using POST and returns the representation from the response.

put

Creates or updates a resource by using PUT.

patchForObject

Updates a resource by using PATCH and returns the representation from the response. Note that the JDK HttpURLConnection does not support the PATCH, but Apache HttpComponents and others do.

delete

Deletes the resources at the specified URI by using DELETE.

optionsForAllow

Retrieves allowed HTTP methods for a resource by using ALLOW.

exchange

More generalized (and less opinionated) version of the preceding methods that provides extra flexibility when needed. It accepts a RequestEntity (including HTTP method, URL, headers, and body as input) and returns a ResponseEntity.

These methods allow the use of ParameterizedTypeReference instead of Class to specify a response type with generics.

execute

The most generalized way to perform a request, with full control over request preparation and response extraction through callback interfaces.

RestTemplate API

要使用RestTmeplate,我们应该创建它的一个实例,或者是注入该类的bean。

获取资源

public Ingredient getIngredientById(String ingredientId){
	return rest.getForObject("http://localhost:8080/api/ingredients/{id}",Ingredient.class,ingredientId);
}

第一个参数指定接口接口的URL,第二个参数指定返回值绑定的类型,第三个参数被绑定到URL中的{id}占位符。

URL参数我们也可以存放在Map中:

public Ingredient getIngredientById(String ingredientId){
	Map<String,Object> params = new HashMap<>();
	params.put("id",ingredientId);
	return rest.getForObject("http://localhost:8080/api/ingredients/{id}",Ingredient.class,params);
}

URL中的“{id}”占位符被params中key为“id”的值所替换。

使用URI参数相对来说要复杂一点:

public Ingredient getIngredientById(String ingredientId){
	Map<String,Object> params = new HashMap<>();
    params.put("id",ingredientId);
    URI url = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/api/tacos/{id}").build(params);
	return rest.getForObject(url,Ingredient.class);
}

我们还可以使用getForEntity方法,该方法返回ResponseBody,包含了出domain对象之外更多的信息。

替换资源

我们使用RestTemplate.put()来替换资源。与getForObjectgetForEntity用法几乎一样,只是该方法的第二个参数是替换的实体对象,如:

public Taco put(){
    Taoc taco = new Taco();
	taco.setName("tacoOne");
	...
    restTemplate.put("http://localhost:8080/rest/design/{id}",taco,12);//将id为12的记录替换为taco
    return taco;
}

删除资源

public void delete(String id){
	restTemplate.delete("http://localhost:8080/rest/design/",id);
}

delete方法只需要两个入参:URL和URL参数。

新建资源

如果我们想要得到创建之后的对象,我们可以使用:postForObject

public Taco create(Taco){
	return taco = restTemplate.postForObject("http://localhost:8080/rest/design",taco,Taco.class);
}

第二个参数为新增资源对象,第三个参数指定响应体的绑定的类型。

如果我们想要得到新增资源的地址,我们可以使用postForLocation

public URI create(Taco taco){
	return restTemplate.postForLocation("http://localhost:8080/rest/design",taco);
}

返回的URI源于响应体的Location Header。

如果两者都想要则使用postForEntity

public Taco create(Taco taco){
	ResponseEntity<Taco> entity = restTemplate.postForEntity("http://localhost:8080/rest/design",taco,Taco.class);
	URI uri = entity.getHeader().getLocation();
	return entity.getBody();
}

上面的例子使用的资源全都不含有超链接,若我们所使用的API返回的资源中含有链接,那么RestTemplate就发挥不了作用了。

使用Tranverson访问REST API

Tranverson同Spring Data HATEOAS协同工作,是使用Spring应用中hypermedia API的解决方案。

首先同RestTemplate一样,我们首选需要一个Tranverson实例,可以实例化一个或者注入Tranversonbean;

Tranverson tranverson = new Tranverson(URI.create("http://localhost:8080/api"),MediaTypes.HAL_JSON);

实例化一个Traverson对象,并指定Spring Data REST API的根路径为起始路径;同时指定我们使用的接口将返回JSON格式的数据,数据包含HAL风格的链接,这样Traverson才能正确解析响应结果。

我们请求API根目录的到的返回结果如下:

访问接口

所以我们可以使用与链接对应的关系名称来访问接口:

tranverson.follow("ingredients");//请求http://localhost:8080/api/ingredients
tranverson.follow("tacos","recent");//请求http://localhost:8080/api/tacos/recent

获取响应结果

ParameterizedTypeReference<CollectionModel<Taco>> tacos = new ParameterizedTypeReference<CollectionModel<Taco>>() {};
CollectionModel<Taco> tacos = traverson.follow("tacos").toObject(tacos);

toObject方法的入参类型为ParameterizedTypeReference<CollectionModel<Taco>>,表示Traverson将会读入CollectionModel<Taco>类型的数据。

因为Traverson发起的所有请求都是HTTP GET类型的,所以我们无法使用Traverson来新增、更新和删除资源。因此,一般情况下我们可能会结合Traverson和RestTemplate一起使用。

String ingredientsUrl = traverson.follow("ingredients").asLink().getHref();
restTemplate.postForObject(ingredientsUrl,ingredient,Ingredient.class);

asLink将会返回资源本身的链接(_links下的self)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值