RestTemplate 使用示例| Id | Title | DateAdded | SourceUrl | PostType | Body | BlogId | Description | DateUpdated | IsMarkdown | EntryName | CreatedTime | IsActive | AutoDesc | AccessPermission |
| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------|
| 15079442| RestTemplate 使用示例| 2021-07-30T14:27:00| | BlogPost|
RestTemplate client = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); //定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON headers.setContentType(MediaType.MULTIPART_FORM_DATA); // Map<String, Object> map = new HashMap<String, Object>(); // map.put("infohash", "1111111"); // map.put("name", "肖申克的救赎12"); // map.put("id", 111111); // map.put("size", 111111); //HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers); MultiValueMap<String,Object> params = new LinkedMultiValueMap<>(); params.add("infohash",infohash.getInfoHash()); params.add("name", infohash.getName()); params.add("id", infohash.getId()); params.add("size", infohash.getLength()); ResponseEntity<String> entity = client.postForEntity("http://localhost:9527/magnet/infohash/setdata", params, String.class); //获取3方接口返回的数据通过entity.getBody();它返回的是一个字符串; String body = entity.getBody();
2
2
String body = null; RestTemplate client = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); MovihInfohashDto infohash = movihInfohashService.create(resources); if(infohash.getId() != null) { MultiValueMap<String,Object> params = new LinkedMultiValueMap<>(); params.add("infohash",infohash.getInfoHash()); params.add("name", infohash.getName()); params.add("id", infohash.getId().toString()); params.add("size", infohash.getLength().toString()); headers.add("Content-Type", "application/x-www-form-urlencoded"); ResponseEntity<String> entity = client.postForEntity(JbibUtils.SETDATA, params, String.class); //获取3方接口返回的数据通过entity.getBody();它返回的是一个字符串; body = entity.getBody();} JSONObject jo </span>=<span style="color: #000000;"> JSONObject.parseObject(body); </span><span style="color: #0000ff;">if</span>(jo != <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(jo.getInteger("code") == 1<span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(jo.getString("msg").equals("CREATED"<span style="color: #000000;">)) { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ResponseEntity<><span style="color: #000000;">(HttpStatus.OK); } } } </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ResponseEntity<><span style="color: #000000;">(HttpStatus.NOT_EXTENDED); </span></pre>
| 648658| | 2021-08-05T16:33:00| false| | 2021-07-30T14:27:02.77| true| RestTemplate client = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); //定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON headers.setContentT| Anonymous| 标签:示例,RestTemplate,add,-------------,params,使用,new,infohash From: https://www.cnblogs.com/ralphlauren/p/18621291