import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
/**
* @author huangbo
*/
@Slf4j
public class TestRestTemplate {
private static RestTemplate restTemplate;
@BeforeAll
static void init() {
/* 构造方法内部自动配置了:
URI模板处理;UriTemplateHandler
消息转换器;HttpMessageConverter
*/
restTemplate = new RestTemplate();
/* StringHttpMessageConverter:
默认 StandardCharsets.ISO_8859_1 ,改成 StandardCharsets.UTF_8 避免乱码 */
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
}
@Test
void postForEntity() {
/* 请求 URL:RestTemplate 会自动对 URL 里的中文进行编码 */
String url = "";
/* url 参数 */
HashMap uriVariables = new HashMap<>();
uriVariables.put("name", "hb");
uriVariables.put("age", String.valueOf(30));
/* 请求头 */
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
// httpHeaders.set(); 覆盖的设置值
// httpHeaders.add(); 添加值
// cookie 请求头格式:key1=value1;key2=value2;key3=value3
httpHeaders.set("cookie","key1=value1;key2=value2;key3=value3");
/* 请求体 */
String jsonStrBody = "{}";
/* 请求头 + 请求体 */
HttpEntity httpEntity = new HttpEntity<>(jsonStrBody, httpHeaders);
/*
postForEntity 返回 ResponseEntity
RestTemplate 请求是同步阻塞的
*/
ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, String.class, uriVariables);
/* 响应:状态码、响应头、响应体 */
HttpStatus statusCode = responseEntity.getStatusCode();
HttpHeaders headers = responseEntity.getHeaders();
String body = responseEntity.getBody();
}
@Test
void postForObject() {
/* 请求 URL:RestTemplate 会自动对 URL 里的中文进行编码 */
String url = "";
/* url 参数 */
HashMap uriVariables = new HashMap<>();
uriVariables.put("name", "hb");
uriVariables.put("age", String.valueOf(30));
/* 请求头 */
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
/* 请求体 */
String jsonStrBody = "{}";
/* 请求头 + 请求体 */
HttpEntity httpEntity = new HttpEntity<>(jsonStrBody, httpHeaders);
/* postForObject 返回 Object */
String obj = restTemplate.postForObject(url, httpEntity, String.class, uriVariables);
}
@Test
void postMultiValueMap() {
String url = "";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap body = new LinkedMultiValueMap<>();
body.add("name","name-1");
body.add("file-1",new File("./test.txt"));
HttpEntity> httpEntity = new HttpEntity<>(body, headers);
ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
}
@Test
void postUseObj() {
/* 请求 URL:RestTemplate 会自动对 URL 里的中文进行编码 */
String url = "";
/* 请求头 */
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.add("user-defined","user-defined");
/* 请求头 + 请求体[由 RestTemplate 提供的 JSON 序列化工具处理] */
Body body = new Body(1L,"user-1",2);
HttpEntity httpEntity = new HttpEntity<>(body, httpHeaders);
/*
postForEntity 返回 ResponseEntity
RestTemplate 请求是同步阻塞的
*/
ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, Result.class);
/* 响应:状态码、响应头、响应体 */
HttpStatus statusCode = responseEntity.getStatusCode();
HttpHeaders headers = responseEntity.getHeaders();
Result responseBody = responseEntity.getBody();
}
@Test
void getPath() {
String path = TestRestTemplate.class.getClassLoader().getResource("./").getPath();
System.out.println(path);
}
@Data
@AllArgsConstructor
static class Body /*implements Serializable*/ {
private Long id;
private String name;
private Integer age;
}
@Data
static class Result{
private Integer code;
private String info;
}
}