接口代码整理
Posted whhjava
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口代码整理相关的知识,希望对你有一定的参考价值。
1.调用接口代码:
(1)http协议工具类:
1 /** 2 * 3 */ 4 package kklazy.test.utils; 5 6 import java.io.IOException; 7 import org.apache.http.HttpEntity; 8 import org.apache.http.StatusLine; 9 import org.apache.http.client.methods.CloseableHttpResponse; 10 import org.apache.http.client.methods.HttpPost; 11 import org.apache.http.entity.StringEntity; 12 import org.apache.http.impl.client.CloseableHttpClient; 13 import org.apache.http.impl.client.HttpClients; 14 import org.apache.http.util.EntityUtils; 15 16 /** 17 * @author whh 18 * http协议工具类 19 */ 20 public class HttpUtils { 21 22 /** 23 * 调用接口 24 * @param url 被调用接口的IP地址 25 * @param msg 上送json数据 26 * @param charset 编码格式 27 * @return 28 */ 29 public static String callInterface(String url, String msg, String charset){ 30 //组装设置请求参数 31 StringEntity requestEntity = new StringEntity(msg, charset); 32 requestEntity.setContentEncoding(charset); 33 requestEntity.setContentType("text/json"); 34 //创建一个httpclient对象 35 CloseableHttpClient httpClient = null; 36 //创建post对象 37 HttpPost httpPost = new HttpPost(url); 38 httpPost.setEntity(requestEntity); 39 String entityStr = null; 40 try { 41 httpClient = HttpClients.createDefault();//创建默认的httpClient实例 42 CloseableHttpResponse httpResponse = httpClient.execute(httpPost);//执行请求 43 try{ 44 HttpEntity entity = httpResponse.getEntity(); 45 entityStr = EntityUtils.toString(entity,"utf-8"); 46 System.out.println("接口响应返回内容:"+entityStr); 47 } finally{ 48 //关闭httpclient连接,释放资源 49 httpResponse.close(); 50 } 51 StatusLine statusLine = httpResponse.getStatusLine(); 52 int statusCode = statusLine.getStatusCode();//获取响应的结果 53 System.out.println("statusCode:"+statusCode); 54 55 } catch (Exception e) { 56 57 }finally{ 58 if(null != httpClient){ 59 try { 60 httpClient.close(); 61 } catch (IOException e) { 62 e.printStackTrace(); 63 } 64 } 65 66 } 67 return entityStr; 68 } 69 70 }
(2)接口参数类:
1 /** 2 * 3 */ 4 package kklazy.test.entity; 5 6 /** 7 * @author whh 8 * 调用接口参数 9 */ 10 public class ApiParam { 11 /** 12 * 13 */ 14 private String version ;//版本号 1.0 15 private String reqType;//交易类型:15 16 private String desc;//说明 17 /** 18 * @return the version 19 */ 20 public String getVersion() { 21 return version; 22 } 23 /** 24 * @param version the version to set 25 */ 26 public void setVersion(String version) { 27 this.version = version; 28 } 29 /** 30 * @return the reqType 31 */ 32 public String getReqType() { 33 return reqType; 34 } 35 /** 36 * @param reqType the reqType to set 37 */ 38 public void setReqType(String reqType) { 39 this.reqType = reqType; 40 } 41 /** 42 * @return the desc 43 */ 44 public String getDesc() { 45 return desc; 46 } 47 /** 48 * @param desc the desc to set 49 */ 50 public void setDesc(String desc) { 51 this.desc = desc; 52 } 53 }
(3)HttpService:
1 package kklazy.test.service; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Map; 8 import org.springframework.stereotype.Service; 9 import org.springframework.transaction.annotation.Transactional; 10 import com.alibaba.fastjson.JSON; 11 import kklazy.test.entity.ApiParam; 12 import kklazy.test.utils.HttpUtils; 13 import net.sf.json.JSONArray; 14 import net.sf.json.JSONObject; 15 16 /** 17 * @author whh 18 */ 19 @Service 20 @Transactional(rollbackFor = Exception.class) 21 public class HttpService { 22 23 public void callInterface(){ 24 ApiParam apiParam = new ApiParam(); 25 apiParam.setVersion("1.0");//版本号 26 apiParam.setReqType("15");//交易类型:固定值15 27 apiParam.setDesc("ABC");//说明 28 Map<String,Object> map = new HashMap<String,Object>(); 29 map.put("requestData",apiParam);//存入map,key为:requestData 30 map.put("signature","111111");//签名:内部使用暂不验签 31 /** 32 * 需要发送的json数据格式为: 33 * {"requestData":{"version":"1.0","reqType":"15","desc":"aaa"},"signature":"11111111"} 34 */ 35 //map转换成json 36 String jsonMsg = JSON.toJSONString(map,true); 37 System.out.println("调用XX系统接口参数值:"+jsonMsg); 38 String responseJson = HttpUtils.callInterface("http://192.168.17.61:9805/UAT/receipt/refund.json", jsonMsg, "UTF-8");//调用接口 39 Map<String, Object> responseMap = parseJSONToMap(responseJson); 40 //获取XX系统返回信息:应答码,响应信息 41 String respCode = (String) responseMap.get("respCode"); 42 String respMsg = (String) responseMap.get("respMsg"); 43 System.out.println("XX系统返回responseMap:应答码:"+respCode+",响应信息:"+respMsg); 44 45 } 46 47 /** 48 * json转换成map类型 49 * @param jsonStr 50 * @return 51 */ 52 public Map<String, Object> parseJSONToMap(String jsonStr){ 53 Map<String,Object> map = new HashMap<String, Object>(); 54 //最外层解析 55 JSONObject json = JSONObject.fromObject(jsonStr); 56 for(Object k :json.keySet()){ 57 Object v = json.get(k); 58 //如果内层还是数组的话,继续解析 59 if(v instanceof JSONArray){ 60 List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); 61 @SuppressWarnings("unchecked") 62 Iterator<JSONObject> it = ((JSONArray)v).iterator(); 63 while(it.hasNext()){ 64 JSONObject json2 = it.next(); 65 list.add(parseJSONToMap(json2.toString())); 66 } 67 map.put(k.toString(), list); 68 }else{ 69 map.put(k.toString(), v); 70 } 71 } 72 return map; 73 } 74 75 }
以上是关于接口代码整理的主要内容,如果未能解决你的问题,请参考以下文章