关于第三方接口调用返回值问题?

GoblinTY 发布于 11/09 17:26
阅读 61
收藏 0

这是第三方接口返回值类型

{

"data": {

"standard": "RO-185I智能-新款",

"filter_period": "",

"channel": "电商",

"type": "1",

"u9_code": "104011-0003",

"pro_list": "",

"pro_install_type": "",

"model": "",

"info": "",

"pro_filter9": "",

"pro_filter8": "",

"pro_filter7": "",

"pro_filter6": "",

"pro_filter5": "190005-0048",

"pro_filter4": "190007-0013",

"pro_filter3": "190005-0002",

"pro_filter2": "190004-0004",

"pro_filter1": "190002-0003",

"water_effect_level": "",

"weight": "",

"market_date": "2017.11",

"suit_filter_count": "",

"hygiene_cert": "",

"size": "197*386*464",

"name": "RO-185I智能-新款",

"pro_filter10": "",

"pro_level": "",

"status": "1",

"suit_filter_list": "",

"electric_cert": "",

"pic1": "",

"is_iot": "",

"pro_org": "",

"pic2": "",

"suning_code": "",

"pic3": "",

"amount": "",

"pro_water": "",

"pro_sublist": "",

"pro_flux": "",

"need_install": "1",

"pro_power": "",

"jingdong_code": "",

"u8_code": "703100"

},

"status": {

"code": "E0",

"message": "成功"

}

}

我请求后的数据为:String类型的字符串。

 

如何返回解析成指定的格式?

加载中
0
GoblinTY
GoblinTY

接口代码:

0
GoblinTY
GoblinTY
@Override
public String findProductsAndFilters(String code) {

    Map<String,String> params = new LinkedHashMap<>();
   //数据查询账户
    String aid=CemUtils.OrderInter.AID_;
    //数据提交时间戳
    long t = System.currentTimeMillis()/1000;
    String ts =String.valueOf(t);
    StringBuilder builder = new StringBuilder("");
    builder.append("aid").append(aid).append("procode").append(code).append("ts").append(ts).append("63f737a925728cfe");
    String sign = MD5Util.MD5Encode(builder.toString(),"utf-8");
    String url = "XXX";
    params.put("aid", CemUtils.OrderInter.AID_);
    params.put("procode",code);
    params.put("ts",ts);
    params.put("sign",sign);


    String result = HttpClientUtil.doGet(url, params);
    return result;
}

 

0
GoblinTY
GoblinTY

HTTP请求代码:

public class HttpClientUtil {
    /**
     * 带参数的get请求
     * @param url
     * @param param
     * @return String
     */
    public static String doGet(String url, Map<String, String> param) {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }
蓝水晶飞机
蓝水晶飞机
CloseableHttpClient 可以用单例,重复创建浪费资源。
0
蓝水晶飞机
蓝水晶飞机

写一个实体类,对应第三方接口的返回的JSON数据格式。

用JSON解析数据,得到实体类,返回实体类给业务调用着。

如果很多这样的接口,建议你试试#OpenFeign# https://github.com/OpenFeign/feign

这货可以帮你省很多事。

0
蓝水晶飞机
蓝水晶飞机

看看 OpenFeign 的例子:

interface GitHub {
  @RequestLine("GET /repos/{owner}/{repo}/contributors")
  List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}

public static class Contributor {
  String login;
  int contributions;
}

public class MyApp {
  public static void main(String... args) {
    GitHub github = Feign.builder()
                         .decoder(new GsonDecoder())
                         .target(GitHub.class, "https://api.github.com");
  
    // Fetch and print a list of the contributors to this library.
    List<Contributor> contributors = github.contributors("OpenFeign", "feign");
    for (Contributor contributor : contributors) {
      System.out.println(contributor.login + " (" + contributor.contributions + ")");
    }
  }
}

是不是很酷?

0
蓝水晶飞机
蓝水晶飞机
interface GitHub {
  @RequestLine("GET /repos/{owner}/{repo}/contributors")
  List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}

public static class Contributor {
  String login;
  int contributions;
}

public class MyApp {
  public static void main(String... args) {
    GitHub github = Feign.builder()
                         .decoder(new GsonDecoder())
                         .target(GitHub.class, "https://api.github.com");
  
    // Fetch and print a list of the contributors to this library.
    List<Contributor> contributors = github.contributors("OpenFeign", "feign");
    for (Contributor contributor : contributors) {
      System.out.println(contributor.login + " (" + contributor.contributions + ")");
    }
  }
}
返回顶部
顶部