“RestTemplate get 方式,getForObject 请求时没有参数”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1AN411Z7mx?p=8”的新页面) |
Jihongchang(讨论 | 贡献) 小 (Jihongchang移动页面RestTemplate get 方式,请求时没有参数至RestTemplate get 方式,getForObject 请求时没有参数) |
||
(未显示同一用户的3个中间版本) | |||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1AN411Z7mx?p=8 | https://www.bilibili.com/video/BV1AN411Z7mx?p=8 | ||
+ | |||
+ | === Application Service 中的 DemoController.java === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import org.springframework.web.bind.annotation.RequestMapping; | ||
+ | import org.springframework.web.bind.annotation.RestController; | ||
+ | |||
+ | @RestController | ||
+ | public class DemoController { | ||
+ | |||
+ | @RequestMapping("/demo") | ||
+ | public String demo() { | ||
+ | return "demo"; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | === Application Client 中的 ClientApplicationTests.java === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import org.junit.jupiter.api.Test; | ||
+ | import org.springframework.boot.test.context.SpringBootTest; | ||
+ | import org.springframework.web.client.RestTemplate; | ||
+ | |||
+ | @SpringBootTest | ||
+ | public class ClientApplicationTests { | ||
+ | |||
+ | @Test | ||
+ | void contextLoads() { | ||
+ | RestTemplate restTemplate = new RestTemplate(); | ||
+ | // 第二个参数也控制该方法返回值类型。因为该方法返回值为响应体数据。 | ||
+ | String result = restTemplate.getForObject("http://localhost:8080/demo", String.class); | ||
+ | System.out.println(result); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> |
2023年3月26日 (日) 06:14的最新版本
https://www.bilibili.com/video/BV1AN411Z7mx?p=8
Application Service 中的 DemoController.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@RequestMapping("/demo")
public String demo() {
return "demo";
}
}
Application Client 中的 ClientApplicationTests.java
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.RestTemplate;
@SpringBootTest
public class ClientApplicationTests {
@Test
void contextLoads() {
RestTemplate restTemplate = new RestTemplate();
// 第二个参数也控制该方法返回值类型。因为该方法返回值为响应体数据。
String result = restTemplate.getForObject("http://localhost:8080/demo", String.class);
System.out.println(result);
}
}