1 @Data 2 class User{ 3 private String name; 4 private Double price; 5 } 6 //get请求无参数 7 @Test 8 void fun1(){ 9 String url = "http://localhost:8082/get"; 10 String result = restTemplate.getForObject(url, String.class); 11 System.out.println(result); 12 } 13 14 //get请求 有参 15 @Test 16 void fun2(){ 17 String url = "http://localhost:8082/getP?name='dfdsf'"; 18 String result = restTemplate.getForObject(url, String.class); 19 System.out.println(result); 20 } 21 @Test 22 void fun3(){ 23 String url = "http://localhost:8082/getP?name='dfdsf'"; 24 ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class); 25 System.out.println(forEntity); 26 } 27 28 @Test 29 void fun4(){ 30 String url = "http://localhost:8082/find"; 31 LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); 32 map.add("name","zhangsna"); 33 map.add("price",9.99D); 34 ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, map, String.class); 35 System.out.println(stringResponseEntity); 36 } 37 38 @Test 39 void fun5(){ 40 String url = "http://localhost:8082/postF"; 41 User user = new User(); 42 user.setName("lisi"); 43 user.setPrice(999D); 44 ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, user, String.class); 45 System.out.println(stringResponseEntity); 46 }
1 @GetMapping("/get") 2 public String find(){ 3 return "ok"; 4 } 5 6 @GetMapping("/getP") 7 public String getP(String name){ 8 return name; 9 } 10 11 @PostMapping("/find") 12 public String postFind(String name,Double price){ 13 return new User(1,name,price).toString(); 14 } 15 16 @PostMapping("/postF") 17 public String postF(@RequestBody User user){ 18 return user.toString(); 19 }
标签:基本,String,url,restTemplate,用法,user,class,name From: https://www.cnblogs.com/ljl5921/p/16611821.html