一、RestTemplate
参考
- 案例一:获取如下图所示的errorCode
public String resultErrorCause(String errorCode) {
String url = "https://www.xfyun.cn/herapi/example/error_code?code=" + errorCode;
ResponseEntity<JSONObject> detectionResponse = restTemplate.exchange(url, HttpMethod.GET, null, JSONObject.class);
JSONObject jsonObject = JSON.parseObject(detectionResponse.getBody().toJSONString());
JSONObject data = jsonObject.getJSONObject("data");
Object errorCause = data.get("errorCause");
return errorCause.toString();
}
2.案例二
获取Object里面的单个属性
public static void test(){
Map<String, Object> resMap = new HashMap<>();
resMap.put("docId", "111");
resMap.put("index", "222");
List<Object> list = new ArrayList<>();
list.add(resMap);
System.out.println(list.get(0));
list.stream().forEach(r->{
JSONObject jsonObject = JSON.parseObject(r.toString().replace("=",":"));
Object index = jsonObject.get("index");
System.out.println(index.toString());
});
}
二、HttpClient
参考