package yuanCheng;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import yuanCheng.bean.NeedInfo;
import yuanCheng.constant.SapConstant;
public class Controller {
public static void main(String[] args) {
// TODO Auto-generated method stub
init("SP","S");
}
public static void init(String company, String cardType) {
//String url=String.format(SapConstant.PARTNER_INIT +"?Company={0}&CardType={1}",company,cardType);
String url = MessageFormat.format(SapConstant.PARTNER_INIT,company,cardType);//封裝請求路徑和請求參數
System.out.println("url==========="+url);
String res = HttpUtil.doGet(url,null);//發送遠程get請求
Map<String,Object> result = JSONObject.parseObject(res, Map.class);//將json串轉換成map類型
System.out.println("result====="+result);
Map<String,Object>data=JSONObject.parseObject(JSONObject.toJSONString(result.get("data")),Map.class);//獲取data對象中的值
List<Map<String,Object>>list=getList(data.get("placeOfDelivery"));//獲取placeOfDelivery中的值轉換成list
System.out.println("list=============="+list);
System.out.println("getlistValue======"+list.get(0));
//將map轉換成實體類中的值
NeedInfo info=JSONObject.parseObject(JSONObject.toJSONString(list.get(0)),NeedInfo.class);//將map中的值放入實體類中,實體類名稱和map字段名稱不一致使用@JSONField(name="lineNum")注解轉換
System.out.println("將名稱不一致字段的值放入對應字段中info============"+info);
}
//將object類型的list轉換成list
public static List<Map<String,Object>>getList(Object obj){
List<Map<String,Object>> result = new ArrayList<>();
if (obj instanceof List<?>) {
for (Object o : (List<?>) obj) {
result.add(Map.class.cast(o));
}
}
result.forEach(System.out::println); // 輸出:1 ab
return result;
}
}
//實體類
package yuanCheng.bean;
import java.io.Serializable;
import com.alibaba.fastjson.annotation.JSONField;
public class NeedInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@JSONField(name="lineNum")
private String line;
@JSONField(name="value")
private String valueName;
public String getLine() {
return line;
}
public void setLine(String line) {
this.line = line;
}
public String getValueName() {
return valueName;
}
public void setValueName(String valueName) {
this.valueName = valueName;
}
@Override
public String toString() {
return "NeedInfo [line=" + line + ", valueName=" + valueName + "]";
}
}
//遠程調用接口util類
package yuanCheng;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
/**
* create by Dell on 2020/6/17
*/
public class HttpUtil {
//get請求
public static String doGet(String url,String authValue){
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-type", "application/json");
if(null!=authValue){
httpGet.setHeader("Authorization","Bearer "+authValue);
}
result = httpClient.execute(httpGet, responseHandler);
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
httpClient.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
return result ;
}
// post請求參數為json格式
public static String doJsonPost(String url, String json,String authValue) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
HttpPost httpPost = new HttpPost(url);
StringEntity requestEntity = new StringEntity(json, "utf-8");
requestEntity.setContentEncoding("UTF-8");
httpPost.setHeader("Content-type", "application/json");
if(null!=authValue){
httpPost.setHeader("Authorization","Bearer "+authValue);
}
httpPost.setEntity(requestEntity);
result = httpClient.execute(httpPost, responseHandler);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
httpClient.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
return result;
}
}
