“RestTemplate get 方式 getForEntity”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的2个中间版本) | |||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1AN411Z7mx?p=10 | https://www.bilibili.com/video/BV1AN411Z7mx?p=10 | ||
− | getForEntity 和 getForObject 的区别在于:getForEntity 包含响应头 | + | getForEntity 和 getForObject 的区别在于:getForEntity 包含响应头<syntaxhighlight lang="java"> |
+ | import org.junit.jupiter.api.Test; | ||
+ | import org.springframework.boot.test.context.SpringBootTest; | ||
+ | import org.springframework.http.ResponseEntity; | ||
+ | import org.springframework.web.client.RestTemplate; | ||
+ | |||
+ | import java.util.HashMap; | ||
+ | import java.util.Map; | ||
+ | |||
+ | @SpringBootTest | ||
+ | public class ClientApplicationTests { | ||
+ | |||
+ | @Test | ||
+ | void getForEntity() { | ||
+ | RestTemplate restTemplate = new RestTemplate(); | ||
+ | ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:8080/demo", String.class); | ||
+ | System.out.println(entity.getBody()); | ||
+ | System.out.println(entity.getStatusCodeValue()); | ||
+ | System.out.println(entity.getStatusCode()); | ||
+ | System.out.println(entity.getHeaders()); | ||
+ | System.out.println(entity.getHeaders().getContentType().toString()); | ||
+ | System.out.println(entity.getHeaders().get("Content-Type").get(0)); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | demo | ||
+ | 200 | ||
+ | 200 OK | ||
+ | [Content-Type:"text/plain;charset=UTF-8", Content-Length:"4", Date:"Sun, 26 Mar 2023 06:27:31 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"] | ||
+ | text/plain;charset=UTF-8 | ||
+ | text/plain;charset=UTF-8 | ||
+ | </syntaxhighlight> |
2023年3月26日 (日) 06:28的最新版本
https://www.bilibili.com/video/BV1AN411Z7mx?p=10
getForEntity 和 getForObject 的区别在于:getForEntity 包含响应头
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
public class ClientApplicationTests {
@Test
void getForEntity() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:8080/demo", String.class);
System.out.println(entity.getBody());
System.out.println(entity.getStatusCodeValue());
System.out.println(entity.getStatusCode());
System.out.println(entity.getHeaders());
System.out.println(entity.getHeaders().getContentType().toString());
System.out.println(entity.getHeaders().get("Content-Type").get(0));
}
}
demo
200
200 OK
[Content-Type:"text/plain;charset=UTF-8", Content-Length:"4", Date:"Sun, 26 Mar 2023 06:27:31 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]
text/plain;charset=UTF-8
text/plain;charset=UTF-8