json 数据转化为实体类 包含list类型

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
 

import net.sf.json.JSONObject;

public class ApolloUtil {
    
     public static String getApollo(String url,String token){
            String result =“”;
            BufferedReader in = null;
            试试{
                URL realUrl = new URL(url);
                //打开和URL之间的连接
                URLConnection connection = realUrl.openConnection();
                connection.setRequestProperty(“Content-Type”,“application / json; charset = UTF-8”);
                connection.setRequestProperty(“授权”,令牌);
                connection.connect();
                in = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                字符串行;
                while((line = in.readLine())!= null){
                    result + = line;
                }
            }赶上(例外五){
                的System.out.println( “发送GET请求出现异常!” + E);
                e.printStackTrace();
            }
            //使用finally块来关闭输入流
            finally {
                try {
                    if(in!= null){
                        in.close();
                    }
                }赶上(例外E2){
                    e2.printStackTrace();
                }
            }
            返回结果;
        }
        @SuppressWarnings(“未选中”)
        @JsonIgnoreProperties(ignoreUnknown = true)
        public static Apollo jsonToApollo(String json){
             Apollo apollo = new Apollo();
            ObjectMapper mapper = new ObjectMapper();
            try {
                Map <String,Object> map = new HashMap <String,Object>();
                map = mapper.readValue(json,new TypeReference <Map <String,Object >>(){
                });
                
                List <Map <String,Object >> stages =(List <Map <String,Object >>)map.get(“items”);
                List <NamespaceItem> itemList = new ArrayList <NamespaceItem>();
                for(Map <String,Object> stage:stages){
                    NamespaceItem namespace = mapper.readValue(mapper.writeValueAsString(stage),NamespaceItem.class);
                    itemList.add(命名空间);
                }
                apollo.setToken((String)map.get(“token”)); 
                apollo.setAppId((字符串)map.get( “APPID”)); 
                apollo.setEnv((字符串)map.get( “ENV”)); 
                apollo.setClusterName((字符串)map.get( “CLUSTERNAME”)); 
                apollo.setNamespaceName((字符串)map.get( “namespaceName”)); 
                apollo.setComment((字符串)map.get( “意见”)); 
                apollo.setFormat((字符串)map.get( “格式化”)); 
                String str = String.valueOf(map。
                if(“true”.equalsIgnoreCase(str)){
                    apollo.setIsPublic(“公有”);
                } else {
                    apollo.setIsPublic(“私有”);
                }
                apollo.setDataChangeCreatedBy((String)map.get(“dataChangeCreatedBy”)); 
                apollo.setDataChangeLastModifiedBy((字符串)map.get( “dataChangeLastModifiedBy”)); 
                apollo.setDataChangeCreatedTime((字符串)map.get( “dataChangeCreatedTime”)); 
                apollo.setDataChangeLastModifiedTime((字符串)map.get( “dataChangeLastModifiedTime”)); 
                apollo.setItems(itemList中);


            } catch(JsonMappingException e){
                e.printStackTrace();
            } catch(IOException e){
                e.printStackTrace();
            }
            返回阿波罗;
        }
        
        公共静态字符串postApollo(字符串字符串,字符串令牌的JSONObject jsonParam){
             
              //获取连接客户端工具
                CloseableHttpClient的HttpClient = HttpClients.createDefault();

                String entityStr = null;
                CloseableHttpResponse response = null;

                尝试{

                    //创建POST请求对象
                    HttpPost httpPost = new HttpPost(string);

                  StringEntity entity = new StringEntity(jsonParam.toString(),“utf-8”); //解决中文乱码问题
                  httpPost.setEntity(entity);

                 //添加请求头信息
                    httpPost.addHeader(“Content-Type”,“application / json; charset = UTF-8”);
                    httpPost.addHeader(“授权”,令牌);
                      
                    //执行请求
                    response = httpClient.execute(httpPost);
                    //获得响应的实体对象
                    
                    HttpEntity entity2 = response.getEntity();
                    //使用Apache提供的工具类进行转换成字符串
                    entityStr = EntityUtils.toString(entity2,“UTF-8”);


                } catch(ClientProtocolException e){
                    System.err.println(“Http协议出现问题”);
                    e.printStackTrace();
                } catch(ParseException e){
                    System.err.println(“解析错误”);
                    e.printStackTrace();
                } catch(IOException e){
                    System.err.println(“IO异常”);
                    e.printStackTrace();
                } finally {
                    //释放连接
                    if(null!= response){
                        try {
                            response.close();
                            httpClient.close();
                        } catch(IOException e){
                            System.err.println(“释放连接出错”);
                            e.printStackTrace();
                        }
                    }
                }
             
         返回entityStr;

         }
        
        @SuppressWarnings(“unused”)
        public static String putApollo(String string,String token,JSONObject jsonParam){
             
            CloseableHttpClient closeableHttpClient = HttpClients.createDefault();  
            
            String content = null;
            CloseableHttpResponse response = null;
            
           
            String url =“”;
            
            HttpPut httpput = new HttpPut(url);  
            
            StringEntity entity = new StringEntity(jsonParam.toString(),“utf-8”); //解决中文乱码问题
            httpput.setEntity(entity);

           //添加请求头信息
              httpput.setHeader(“Content-Type”,“application / json; charset = UTF-8”);
              httpput.setHeader(“授权”,令牌);
            
            try {    
             //响应信息  
                response = closeableHttpClient.execute(httpput);    
                HttpEntity entity2 = response.getEntity();
                
                content = EntityUtils.toString(entity,“UTF-8”);
                
                //使用Apache提供的工具类进行转换成字符串
            } catch(ClientProtocolException e){
                System.err.println(“Http协议出现问题”);
                e.printStackTrace();
            } catch(ParseException e){
                System.err.println(“解析错误”)
                e.printStackTrace();
            } catch(IOException e){
                System.err.println(“IO异常”);
                e.printStackTrace();
            } finally {
                //释放连接
                if(null!= response){
                    try {
                        response.close();
                        closeableHttpClient.close();
                    } catch(IOException e){
                        System.err.println(“释放连接出错”);
                        e.printStackTrace();
                    }
                }
            }
         
     返回的内容;
     
    }

}

猜你喜欢

转载自blog.csdn.net/stone_tomcate/article/details/81744777