1.RestTemplate
RestTemplate 使用详解
RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程 Http 服务的方法,能够大大提高客户端的编写效率。
也就是后端利用 RestTemplate 发 http 请求,去访问其他微服务的 controller(类似于前端的 axios 发送请求)
之前的 HTTP 开发是用 apache 的 HttpClient 开发,代码复杂,还得操心资源回收等。代码很复杂,冗余代码多。
2.快速使用
SpringBoot 依赖 2.7.1 版本:我们只需要导入 web 启动器即可
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
我们来进行编写三种案例:
①GET
②POST JSON
③POST 表单
java
package com.changlu.resttemplate.controller;
import com.changlu.resttemplate.domain.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description: 测试控制器
* @Author: changlu
* @Date: 4:04 PM
*/
@RestController
public class TestController {
/**
* 接收?&的参数,什么都不用加
*/
@GetMapping("/testGet")
public String get(String name) {
System.out.println(name);
return "ok";
}
/**
* post传参 两种:json和表单
* json参数:header :content-type = application/json;charset=utf-8
* 接收json加个@RequestBody注解即可
* @return
*/
@PostMapping("/testPost1")
public String testPost1(@RequestBody User user) {
System.out.println(user);
return "ok";
}
/**
* 表单参数:header :content-type = application/x-www-form-urlencoded
* <form action=/testPost2
* <input name=ccc value=ssss/>
* @param user
* @return
*/
@PostMapping("/testPost2")
public String testPost2(User user) {
System.out.println(user);
return "ok";
}
}
接着我们来使用测试类 RestTemplate 来进行测试:分别对应三个请求
java
package com.changlu.resttemplate;
import com.changlu.resttemplate.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
@SpringBootTest
class RestTemplateApplicationTests {
@Test
void test() {
RestTemplate restTemplate = new RestTemplate();
//访问百度页面
String url = "https://www.baidu.com";
String content = restTemplate.getForObject(url, String.class); //getForObject返回的http响应中的结果那一项,返回类型是泛型,也就是任意类型,String.class就是返回的类型
System.out.println(content); //返回html代码
}
@Test
void testGet() {
RestTemplate restTemplate = new RestTemplate();
String url = "localhost:8080/testGet?name=cxs";
// String result = restTemplate.getForObject(url, String.class);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class); //getForEntity返回的是整个http响应的信息
// http协议 (规范、接头暗号)
// 包括:请求头 请求参数 .. 响应头 响应状态码 报文 ....
System.out.println(responseEntity);
}
@Test
void testPostJson() {
RestTemplate restTemplate = new RestTemplate();
String url = "localhost:8080/testPost1";
//封装对象
User user = new User("changlu", 22, 1000D);
//发送POST,而且是JSON参数,直接传对象即可,因为sb在web里面默认会使用jackson,会将对象转为字符串
String content = restTemplate.postForObject(url, user, String.class);
System.out.println(content);
}
@Test
void testPost() {
RestTemplate restTemplate = new RestTemplate();
String url = "localhost:8080/testPost2";
//对于表单参数,使用LinkedMultiValueMap来进行封装!!!
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("name", "changlu");
map.add("age", 26);
map.add("price", 80080);
String content = restTemplate.postForObject(url, map, String.class);
System.out.println(content);
}
}