Java爬坑 -- spring boot + RestTemplate
最近从HTTP跳 RestTemplate ,,,----------直接上代码...仅供参考,,,还请各位大神指正
1 import com.alibaba.fastjson.JSONObject; 2 import com.chuzihang.model.UserInfo; 3 import org.apache.commons.lang3.RandomUtils; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.http.HttpHeaders; 8 import org.springframework.http.HttpStatus; 9 import org.springframework.http.ResponseEntity; 10 import org.springframework.web.bind.annotation.*; 11 import org.springframework.web.client.RestTemplate; 12 import org.springframework.web.util.UriComponents; 13 import org.springframework.web.util.UriComponentsBuilder; 14 15 import java.net.URI; 16 17 /** 18 * @ClassName TestController 19 * @Description TODO 20 * @Author Q_先生 21 * @Date 2018/7/10 16:46 22 **/ 23 @RestController 24 @RequestMapping("/test") 25 public class TestController { 26 27 private Logger logger = LoggerFactory.getLogger(getClass()); 28 29 @Autowired 30 private RestTemplate restTemplate; 31 32 /********** HTTP GET method **************/ 33 34 @GetMapping("/get") 35 public String get() { 36 String url = "http://localhost:8080/test/getApi"; 37 ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class); 38 String body = responseEntity.getBody(); 39 HttpStatus statusCode = responseEntity.getStatusCode(); 40 int statusCodeValue = responseEntity.getStatusCodeValue(); 41 HttpHeaders headers = responseEntity.getHeaders(); 42 StringBuilder result = new StringBuilder(); 43 result.append("responseEntity.getBody():").append(body).append("<hr>") 44 .append("responseEntity.getStatusCode():").append(statusCode).append("<hr>") 45 .append("responseEntity.getStatusCodeValue():").append(statusCodeValue).append("<hr>") 46 .append("responseEntity.getHeaders():").append(headers).append("<hr>"); 47 return result.toString(); 48 } 49 50 @GetMapping("/get1") 51 public String get1() { 52 String param = "测试啊真的"; 53 String url = "http://localhost:8080/test/getApi1?param={1}"; 54 JSONObject json = restTemplate.getForEntity(url, JSONObject.class, param).getBody(); 55 return json.toJSONString(); 56 } 57 58 @GetMapping("/get2") 59 public String get2() { 60 String param = "测试啊真的"; 61 JSONObject getData = new JSONObject(); 62 getData.put("param", param); 63 String url = "http://localhost:8080/test/getApi1?param={param}"; 64 JSONObject json = restTemplate.getForEntity(url, JSONObject.class, getData).getBody(); 65 return json.toJSONString(); 66 } 67 68 @GetMapping("/get3") 69 public String get3() { 70 UriComponents uriComponents = 71 UriComponentsBuilder.fromUriString("http://localhost:8080/test/getApi1?param={name}") 72 .build() 73 .expand("测试啊真的") 74 .encode(); 75 URI uri = uriComponents.toUri(); 76 ResponseEntity<String> responseEntity = restTemplate.getForEntity(uri, String.class); 77 return responseEntity.getBody(); 78 } 79 80 @GetMapping("/getApi") 81 public Object getApi() { 82 JSONObject json = new JSONObject(); 83 json.put("descp", "this is spring rest template sample"); 84 return json; 85 } 86 87 @GetMapping("/getApi1") 88 public Object genJson(@RequestParam String param) { 89 logger.info("==========================" + param); 90 JSONObject json = new JSONObject(); 91 json.put("descp", "this is spring rest template sample"); 92 return json; 93 } 94 95 /********** HTTP POST method **************/ 96 97 /** 98 * @Author Q_先生 99 * 第一种:postForEntity 100 * 第二种:postForObject 101 * 如果你只关注,返回的消息体,可以直接使用postForObject。用法和getForObject一致。 102 * 第三种:postForLocation 103 * postForLocation也是提交新资源,提交成功之后,返回新资源的URI,postForLocation的参数和前面两种的参数基本一致, 104 * 只不过该方法的返回值为Uri,这个只需要服务提供者返回一个Uri即可,该Uri表示新资源的位置。 105 **/ 106 @PostMapping("/postApi") 107 public UserInfo iAmPostApi(@RequestBody UserInfo userInfo) { 108 logger.info("username==================" + userInfo.getUserName()); 109 userInfo.setId(RandomUtils.nextInt(1, 100)); 110 return userInfo; 111 } 112 113 @PostMapping("/post") 114 public Object testPost() { 115 UserInfo userInfo = new UserInfo(); 116 userInfo.setUserName("楚子航"); 117 String url = "http://localhost:8080/test/postApi"; 118 ResponseEntity<UserInfo> responseEntity = restTemplate.postForEntity(url, userInfo, UserInfo.class); 119 return responseEntity.getBody(); 120 } 121 122 /********** HTTP PUT method 没有具体实现 **************/ 123 124 @RequestMapping("/put") 125 public void put() { 126 UserInfo userInfo = new UserInfo(); 127 userInfo.setUserName("路明非"); 128 restTemplate.put("http://localhost:8080/test/postApi1/{1}", userInfo, 99); 129 } 130 131 /********** HTTP DELETE method 没有具体实现 **************/ 132 133 @RequestMapping("/delete") 134 public void delete() { 135 restTemplate.delete("http://localhost:8080/test/postApi2/{1}", 100); 136 } 137 138 }