1 Java 使用 RestTemplate 调取第三方 HTTP
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/RestTemplateTest")
public class RestTemplateTestController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/restTemplatelist")
@ResponseBody
public String restTemplatelist() {
Map map = new HashMap<>();
map.put("imei", "861854049739673");
// 设置字符编码
restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
HttpHeaders headers = new HttpHeaders();
headers.set("api-key", "NDbJtnvOZILNxXmLtUYH=a981JQ=");
String imeiNum = "861854049739673";
// 注意几个请求参数
ResponseEntity res =
restTemplate.exchange("http://api.heclouds.com/devices/getbyimei?imei="+imeiNum+"",
HttpMethod.GET, new HttpEntity<>(null, headers), String.class);
// JSonObject data = JSONObject.parseObject(res.getBody()).getJSonObject("data");
// 将所获复杂(对象内包对象)json转为Java对象调用。
All imeiMsg = JSONObject.parseObject(res.getBody(), All.class);
return imeiMsg.getData().getOnline();
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
2 复杂(对象内包对象)json转为Java对象
{
"errno": 0,
"error": "succ",
"data": {
"create_time": "2021-07-05 15:08:37",
"online": false,
"id": "742851839",
"observe_status": false,
"title": "861854049739673"
}
}
- Msg
public class Msg {
String errno;
String error;
public Msg(String errno, String error) {
this.errno = errno;
this.error = error;
}
public String getErrno() { return errno; }
public void setErrno(String errno) { this.errno = errno; }
public String getError() { return error; }
public void setError(String error) { this.error = error; }
}
- ItemData
package com.shylFm.web.controller.httpConnection;
public class ItemData{
String create_time;
String online;
String id;
String observe_status;
String title;
public String getCreate_time() { return create_time; }
public void setCreate_time(String create_time) { this.create_time = create_time; }
public String getOnline() { return online; }
public void setOnline(String online) { this.online = online; }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getObserve_status() { return observe_status; }
public void setObserve_status(String observe_status) { this.observe_status = observe_status; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
}
- All
public class All extends Msg{
private ItemData data;
public All(String errno, String error, ItemData data) {
super(errno, error);
this.data = data;
}
public ItemData getData() { return data; }
public void setData(ItemData data) { this.data = data; }
}
- 复杂(对象内包对象)json转为Java对象调用实例
import com.alibaba.fastjson.JSONObject;
public class test {
public static void main(String[] args) {
String str = "{"errno":0,"error":"succ","data":{"create_time":"2021-07-05 15:08:37","online":false,"id":"742851839","observe_status":false,"title":"861854049739673"}}";
All all = JSONObject.parseObject(str, All.class);
System.out.println(all.getData().getId());
}
}