調用其他人提供的API的參考鏈接:https://www.baeldung.com/rest-template
下面代碼是對於JSON字符串格式為這種的: {"Table":[{"name":"a1","code":"aaa"},{"name":"b1","code":"bbb"}]}
1、
@GetMapping("/getAll") private List<Smcorp> getAll() throws JsonProcessingException { //調用接口的鏈接 final String url = "http://xxx.xxx.x.x:xxxx/api/xxxxx/GetAll"; RestTemplate restTemplate = new RestTemplate(); //獲取API返回值 String forObject = restTemplate.getForObject(url, String.class); List<Smcorp> smcorps = new ArrayList<>(); if (forObject != null) { //如果JSON字符串里面有換行符等,需要去掉,要不然會報錯 forObject = forObject.replaceAll("[\b\r\n\t]*", ""); //也可以用substring截取數組那一段,速度會比下面這個方法快 Object table = new JSONObject(forObject).get("Table");
//忽略那些映射不對應的字段
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//字符串形式的對象數組轉成實體類對象集合
smcorps = new ObjectMapper().readValue(table.toString(), new TypeReference<List<Smcorp>>() { }); } return smcorps; }
本地建立一個實體類與第三方數據庫返回的Json數據與之對應
public class Gldocs { @JsonIgnore//把該實體類的數據返回前端時,這個字段不序列化成json返回前端,即前端拿不到這個字段 @JsonProperty("CORPTYPE")//序列化成JSON時的名字,前端調用是調用這個名字CORPTYPE private String corpType; @JsonProperty("SDOC_ID") private String sdocId; public String getCorpType() { return corpType; } public void setCorpType(String corpType) { this.corpType = corpType; } public String getSdocId() { return sdocId; } public void setSdocId(String sdocId) { this.sdocId = sdocId; } }
2、
@GetMapping("/getAll") private List<Smcorp> getAll2() throws JsonProcessingException { //調用接口的鏈接 final String url = "http://xxx.xxx.x.x:xxxx/api/xxxx/GetAll"; RestTemplate restTemplate = new RestTemplate(); //獲取API返回值 String forObject = restTemplate.getForObject(url, String.class); List<Smcorp> smcorps = new ArrayList<>(); if (forObject != null) { //用substring截取數組字符串 forObject=forObject.substring(9, forObject.length() - 1); //如果JSON字符串里面有換行符等,需要去掉,要不然會報錯 forObject = forObject.replaceAll("[\b\r\n\t]*", ""); //字符串形式的對象數組轉成實體類對象集合 smcorps = new ObjectMapper().readValue(forObject , new TypeReference<List<Smcorp>>() { }); } return smcorps; }
3、
@GetMapping("/getAll") private List<Smcorp> getAll2() throws JsonProcessingException { //調用接口的鏈接 final String url = "http://xxx.xxx.x.x:xxxx/api/xxxx/GetAll"; RestTemplate restTemplate = new RestTemplate(); //獲取API返回值 String forObject = restTemplate.getForObject(url, String.class); List<Smcorp> smcorps = new ArrayList<>(); if (forObject != null) { //如果JSON字符串里面有換行符等,需要去掉,要不然會報錯 forObject = forObject.replaceAll("[\b\r\n\t]*", ""); } //把字符串轉成JSONObject JSONObject jsonObject = new JSONObject(forObject); //通過key拿到JSONArray JSONArray table = jsonObject.getJSONArray("Table"); //把JSONArray轉成List<Object> List<Object> objects = table.toList(); //把List<Object>轉成實體類對象集合 smcorps = objects.stream().map(x -> new ObjectMapper().convertValue(x, Smcorp.class)).collect(Collectors.toList()); return smcorps; }
速度最快的是第2種方法,然后是第一種。直接用 substring截取最快,用這個 new ObjectMapper().readValue(forObject , new TypeReference<List<Smcorp>>() })把字符串形式的對象數組轉成實體類集合也很快。
4、調用接口傳參,使用map設置好鍵值對,再調用RestTemplate
String url = "http://xx.xxx.x.xx:xxxx/api/xxxx/xxxx/corptype={corptype}&SCORPCODE={scorpcode}&SACC_CODE_C={saccCodeC}&SPERIODCODE={speriodCode}"; HashMap<String, String> map = new HashMap<>(); map.put("corptype", corptype); map.put("scorpcode", scorpcode); map.put("saccCodeC", saccCodeC); map.put("speriodCode", speriodCode); RestTemplate restTemplate = new RestTemplate(); String forObject = restTemplate.getForObject(url, String.class, map);