后端调用其他人提供的接口,把得到的JSON字符串数据反序列化成对象或数组

 调用其他人提供的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("[
	]*", "");
            //也可以用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("[
	]*", "");
            //字符串形式的对象数组转成实体类对象集合
            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("[
	]*", "");
        }
        //把字符串转成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);
               
原文地址:https://www.cnblogs.com/pzw23/p/13566402.html