“RestTemplate get 方式,getForObject 请求时带有参数的6种情况”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第12行: | 第12行: | ||
public String demo2(String name, Integer age) { | public String demo2(String name, Integer age) { | ||
return "name:" + name + ",age:" + age; | return "name:" + name + ",age:" + age; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </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 withParam() { | ||
+ | RestTemplate restTemplate = new RestTemplate(); | ||
+ | String result = restTemplate.getForObject("http://localhost:8080/demo2?name=jihch&age=15", String.class); | ||
+ | System.out.println(result); | ||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
2023年3月26日 (日) 05:48的版本
https://www.bilibili.com/video/BV1AN411Z7mx?p=9
Application Service 中的 DemoController.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@RequestMapping("/demo2")
public String demo2(String name, Integer age) {
return "name:" + name + ",age:" + age;
}
}
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 withParam() {
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://localhost:8080/demo2?name=jihch&age=15", String.class);
System.out.println(result);
}
}