Java调取第三方HTTP,并将所获复杂(对象内包对象)json转为Java对象调用。

2021/9/27 17:11:03

本文主要是介绍Java调取第三方HTTP,并将所获复杂(对象内包对象)json转为Java对象调用。,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1 Java 使用 RestTemplate 调取第三方 HTTP

  • RestTemplate使用实例
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<String, String> 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<String> 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对象

  • json字符串示例
{
    "errno": 0,
    "error": "succ", 
    "data": { 
      	 "create_time": "2021-07-05 15:08:37", 
     	 "online": false,
     	 "id": "742851839", 
     	 "observe_status": false, 
    	 "title": "861854049739673" 
    }
}
  • 实体类整合
    • 使用继承方法,避免嵌套,产生静态及内部的歧义。
  1. 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; }
}
  1. 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; }
}
  1. 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());
    }
}


这篇关于Java调取第三方HTTP,并将所获复杂(对象内包对象)json转为Java对象调用。的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程