springboot学习18

Spring 应用程序可以通过以下方式使用 REST API:
一、RestTemplate
由 Spring 核心框架提供、简单、同步 REST 客户端。
RestTemplate 定义操作:

方法描述
delete(…)对指定 URL 上的资源执行 HTTP DELETE请求
exchange(…)对 URL 执行指定的 HTTP 方法,返回一个 ResponseEntity,其中包含从响应体映射的对象
execute(…)对 URL 执行指定的 HTTP 方法,返回一个映射到响应体的对象
getForEntity(…)发送 HTTP GET 请求,返回一个 ResponseEntity,其中包含从响应体映射的对象
getForObject(…)发送 HTTP GET 请求,返回一个映射到响应体的对象
headForHeaders(…)发送 HTTP HEAD 请求,返回指定资源 URL 的 HTTP 请求头
optionsForAllow(…)发送 HTTP OPTIONS 请求,返回指定 URL 的 Allow 头信息
patchForObject(…)发送 HTTP PATCH 请求,返回从响应主体映射的结果对象
postForEntity(…)将数据 POST 到一个 URL,返回一个 ResponseEntity,其中包含从响应体映射而来的对象
postForLocation(…)将数据 POST 到一个 URL,返回新创建资源的 URL
postForObject(…)将数据 POST 到一个 URL,返回从响应主体映射的对象
put(…)将资源数据 PUT 到指定的URL

使用 RestTemplate,需要创建一个实例:

RestTemplate rest = new RestTemplate();

或者bean方式注入:

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

1、请求 GET 资源
使用 getForObject() 来获取 User:
1)

public User getUserById(String userId) {
    return rest.getForObject("http://localhost:8080/users/{id}",
                             User.class, userId);
}

2)使用映射来指定 URL 变量:

public User getUserById(String userId) {
    Map<String, String> urlVariables = new HashMap<>();
    urlVariables.put("id", userId);
    return rest.getForObject("http://localhost:8080/users/{id}",
                            User.class, urlVariables);
}

3)使用 URI 参数:

public User getUserById(String userId) {
    Map<String,String> urlVariables = new HashMap<>();
    urlVariables.put("id", userId);
    URI url = UriComponentsBuilder
        .fromHttpUrl("http://localhost:8080/users/{id}")
        .build(urlVariables);
    return rest.getForObject(url, User.class);
}

使用getForEntity() :

public User getUserById(String userId) {
    ResponseEntity<User> responseEntity =
        rest.getForEntity("http://localhost:8080/users/{id}",
                          User.class, userId);
    
    log.info("Fetched time: " +
             responseEntity.getHeaders().getDate());
    
    return responseEntity.getBody();
}

2、请求 PUT 资源

public void updateUser(User user) {
    rest.put("http://localhost:8080/users/{id}",
            user,
            user.getId());
}

3、请求 DELETE 资源
将 URL(指定为 String)和 URL 变量值赋给 delete(),如下:

public void deleteUser(User user) {
    rest.delete("http://localhost:8080/users/{id}",
               user.getId());
}

RestTemplate 方法一样,可以将 URL 指定为 URI 对象,或者将 URL 参数指定为 Map。

4、请求 POST 资源
在 POST 请求后收到新创建的User 资源:

public User createIngredient(User user) {
    return rest.postForObject("http://localhost:8080/users",
                             user,
                             User.class);
}

返回新创建的资源的位置如postForLocation():

public URI createUser(User user) {
    return rest.postForLocation("http://localhost:8080/users",
                                user);
}

返回位置和响应负载,postForEntity():

public User createUser(User user) {
    ResponseEntity<User> responseEntity =
        rest.postForEntity("http://localhost:8080/users",
                           user,
                           User.class);
    
    log.info("New resource created at " +
             responseEntity.getHeaders().getLocation());
    
    return responseEntity.getBody();
}

二、Traverson
感知超链接的同步 REST 客户端,由 Spring HATEOAS 提供。
实例化一个 Traverson 对象和一个 API 的基础 URI:

Traverson traverson = new Traverson(
    URI.create("http://localhost:8080/api"), MediaType.HAL_JSON);

三、WebClient
Spring 5 中引入的响应式、异步 REST 客户端。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值