“RestTemplate get 方式,getForObject 请求时带有参数的6种情况”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) 小 (Jihongchang移动页面RestTemplate get 方式,请求时带有参数的6种情况至RestTemplate get 方式,getForObject 请求时带有参数的6种情况) |
||
(未显示同一用户的6个中间版本) | |||
第8行: | 第8行: | ||
@RestController | @RestController | ||
public class DemoController { | public class DemoController { | ||
+ | |||
+ | @RequestMapping("/demo") | ||
+ | public String demo() { | ||
+ | return "demo"; | ||
+ | } | ||
+ | |||
@RequestMapping("/demo2") | @RequestMapping("/demo2") | ||
public String demo2(String name, Integer age) { | public String demo2(String name, Integer age) { | ||
+ | return "name:" + name + ",age:" + age; | ||
+ | } | ||
+ | |||
+ | |||
+ | @RequestMapping("/demo3/{name}/{age}") | ||
+ | public String demo3(@PathVariable String name, @PathVariable Integer age) { | ||
return "name:" + name + ",age:" + age; | return "name:" + name + ",age:" + age; | ||
} | } | ||
第16行: | 第28行: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
=== Application Client 中的 ClientApplicationTests.java === | === Application Client 中的 ClientApplicationTests.java === | ||
第25行: | 第44行: | ||
@SpringBootTest | @SpringBootTest | ||
public class ClientApplicationTests { | public class ClientApplicationTests { | ||
+ | |||
+ | @Test | ||
+ | void contextLoads() { | ||
+ | RestTemplate restTemplate = new RestTemplate(); | ||
+ | // 第二个参数也控制该方法返回值类型。因为该方法返回值为响应体数据。 | ||
+ | String result = restTemplate.getForObject("http://localhost:8080/demo", String.class); | ||
+ | System.out.println(result); | ||
+ | } | ||
@Test | @Test | ||
第30行: | 第57行: | ||
RestTemplate restTemplate = new RestTemplate(); | RestTemplate restTemplate = new RestTemplate(); | ||
String result = restTemplate.getForObject("http://localhost:8080/demo2?name=jihch&age=15", String.class); | String result = restTemplate.getForObject("http://localhost:8080/demo2?name=jihch&age=15", String.class); | ||
+ | System.out.println(result); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | void withParam2() { | ||
+ | RestTemplate restTemplate = new RestTemplate(); | ||
+ | String result = restTemplate.getForObject("http://localhost:8080/demo2?name={1}&age={2}", String.class, "jihch", 15); | ||
+ | System.out.println(result); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | void withParam3() { | ||
+ | Map<String, Object> map = new HashMap<>(); | ||
+ | map.put("name", "jihch"); | ||
+ | map.put("age", 15); | ||
+ | RestTemplate restTemplate = new RestTemplate(); | ||
+ | String result = restTemplate.getForObject("http://localhost:8080/demo2?name={name}&age={age}", String.class, map); | ||
+ | System.out.println(result); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | void withParam4() { | ||
+ | RestTemplate restTemplate = new RestTemplate(); | ||
+ | String result = restTemplate.getForObject("http://localhost:8080/demo3/jihch/15", String.class); | ||
+ | System.out.println(result); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | void withParam5() { | ||
+ | RestTemplate restTemplate = new RestTemplate(); | ||
+ | String result = restTemplate.getForObject("http://localhost:8080/demo3/{1}/{2}", String.class, "jihch", 15); | ||
+ | System.out.println(result); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | void withParam6() { | ||
+ | Map<String, Object> map = new HashMap<>(); | ||
+ | map.put("name", "jihch"); | ||
+ | map.put("age", 15); | ||
+ | RestTemplate restTemplate = new RestTemplate(); | ||
+ | String result = restTemplate.getForObject("http://localhost:8080/demo3/{name}/{age}", String.class, map); | ||
System.out.println(result); | System.out.println(result); | ||
} | } | ||
} | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | name:jihch,age:15 | ||
</syntaxhighlight> | </syntaxhighlight> |
2023年3月26日 (日) 06:14的最新版本
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("/demo")
public String demo() {
return "demo";
}
@RequestMapping("/demo2")
public String demo2(String name, Integer age) {
return "name:" + name + ",age:" + age;
}
@RequestMapping("/demo3/{name}/{age}")
public String demo3(@PathVariable String name, @PathVariable 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 contextLoads() {
RestTemplate restTemplate = new RestTemplate();
// 第二个参数也控制该方法返回值类型。因为该方法返回值为响应体数据。
String result = restTemplate.getForObject("http://localhost:8080/demo", String.class);
System.out.println(result);
}
@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);
}
@Test
void withParam2() {
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://localhost:8080/demo2?name={1}&age={2}", String.class, "jihch", 15);
System.out.println(result);
}
@Test
void withParam3() {
Map<String, Object> map = new HashMap<>();
map.put("name", "jihch");
map.put("age", 15);
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://localhost:8080/demo2?name={name}&age={age}", String.class, map);
System.out.println(result);
}
@Test
void withParam4() {
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://localhost:8080/demo3/jihch/15", String.class);
System.out.println(result);
}
@Test
void withParam5() {
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://localhost:8080/demo3/{1}/{2}", String.class, "jihch", 15);
System.out.println(result);
}
@Test
void withParam6() {
Map<String, Object> map = new HashMap<>();
map.put("name", "jihch");
map.put("age", 15);
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://localhost:8080/demo3/{name}/{age}", String.class, map);
System.out.println(result);
}
}
name:jihch,age:15