如何開放接口(三要素:地址;入參;響應數據)

在這裏插入圖片描述

在這裏插入圖片描述
Postman演示效果(測試你寫的接口能不能調取數據成功),反過來測試你接口能不能使用:

在這裏插入圖片描述

本示例用struts2演示:

1.編寫struts2配置文件,聲明action類

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 微信授權註冊模塊 -->
<package name="dzservice" namespace="/dzservice" extends="struts-default">
		<action name="orderMessage" class="com.action.gzh.dzAction" method="orderMessage"></action>
		<action name="accessRecords" class="com.action.gzh.dzAction" method="accessRecords"></action>
		<action name="otherPayment" class="com.action.gzh.dzAction" method="otherPayment"></action>
		<action name="transactionRecords" class="com.action.gzh.dzAction" method="transactionRecords"></action>
</package>
</struts>

2.編寫action類

  /**
     * 交易記錄
     */
    public void transactionRecords(){
    	ApplicationContext context = new ClassPathXmlApplicationContext("applicationBt.xml");
    	JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
    	final JSONArray arr=new JSONArray();
    	 StringBuffer sb = new StringBuffer("");
 		try {
 			BufferedReader br = new BufferedReader(new InputStreamReader(this.getRequest().getInputStream(), "utf-8"));
 	        String temp;
 	        while ((temp = br.readLine()) != null) {
 	            sb.append(temp);
 	        }
 	        br.close();
 		} catch (UnsupportedEncodingException e1) {
 			// TODO Auto-generated catch block
 			e1.printStackTrace();
 		} catch (IOException e1) {
 			// TODO Auto-generated catch block
 			e1.printStackTrace();
 		}
 		JSONObject js =JSONObject.fromObject(sb.toString());
 		
 		
 		
		System.out.println("val=="+sb.toString());
    	String payer_userid = js.get("payer_userid").toString();  //用戶主鍵  NEWID
    	String b_time = js.get("b_time").toString();  //開始日期
    	String e_time = js.get("e_time").toString();  //結束日期
    	PrintWriter out=null;
    	try {
    		this.getResponse().setCharacterEncoding("utf-8");
			out = this.getResponse().getWriter();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	//判斷主鍵是否存在
    	String sql="select bill_no,c.device_guid,a.AccountNo,pay_money,c.oper_date,a.bank_id,c.pay_from,c.remark from cust_charge c " +
    			   "join bankDetail a on a.sequenceNo=c.bill_no where device_guid=? and substring(c.oper_date,1,10)>=? and substring(c.oper_date,1,10)<=?";
    	Object[] sqlobj = new Object[]{
    			payer_userid,b_time,e_time};
    	jdbcTemplate.query(sql,sqlobj,  new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				final JSONObject json=new JSONObject();
				json.put("order_no",rs.getString("bill_no"));
				json.put("payer_userid",rs.getString("device_guid"));
				json.put("receiver_userid",rs.getString("AccountNo"));
				json.put("money",rs.getString("pay_money"));
				json.put("pay_time",rs.getString("oper_date"));
				String bank_type=rs.getString("bank_id");
				if(bank_type.equals("00")){
					json.put("bank_type","光大行");
				}else if(bank_type.equals("01")){
					json.put("bank_type","寧波行");
				}else if(bank_type.equals("02")){
					json.put("bank_type","農行");
				}else if(bank_type.equals("03")){
					json.put("bank_type","建行");
				}else{
					json.put("bank_type","");
				}
				String charge_type=rs.getString("pay_from");
				if(charge_type.equals("0")){
					json.put("charge_type","一卡通");
				}else if(charge_type.equals("1")){
					json.put("charge_type","微信");
				}else if(charge_type.equals("2")){
					json.put("charge_type","APP");
				}else if(charge_type.equals("3")){
					json.put("charge_type","本地");
				}else if(charge_type.equals("4")){
					json.put("charge_type","單機");
				}else if(charge_type.equals("5")){
					json.put("charge_type","其他");
				}else if(charge_type.equals("6")){
					json.put("charge_type","支付寶");
				}else if(charge_type.equals("7")){
					json.put("charge_type","網銀");
				}else{
					json.put("charge_type","");
				}
				json.put("remark",rs.getString("remark"));
				arr.add(json);
			}
    		
    	});
    	if(arr.size()>0){
			//插入支付信息
			JSONObject json = new JSONObject();
        	json.put("msg", "success");
        	json.put("data", arr.toString());
			out.write(json.toString());
    	}else{
    		JSONObject json = new JSONObject();
        	json.put("msg", "no");
    		out.write(json.toString());
    	}
	    out.flush();
	    out.close();	
    }

3.提供接口地址 :
URI: http://110.00.63.90:8081/jdy/dzservice/transactionRecords
接口參數: 定義爲string 或者 json,本示例定義爲json

{
	"payer_userid": "61ABF5D4-7D94-6103-0ED3-9D15CDBAC4EF",
	"b_time": "2020-02-25",
	"e_time": "2020-02-26"
}

返回值 示例:

{"msg":"success","data":[{"bank_type":"","charge_type":"微信","money":"0.01","order_no":"gzw1582603738656908","pay_time":"2020-02-25 12:08:58","payer_userid":"61ABF5D4-7D94-6103-0ED3-9D15CDBAC4EF","receiver_userid":"32050161503600001380-2222","remark":"其他說明"},{"bank_type":"","charge_type":"微信","money":"0.01","order_no":"gzw1582600602980495","pay_time":"20200225111642980","payer_userid":"61ABF5D4-7D94-6103-0ED3-9D15CDBAC4EF","receiver_userid":"32050161503600001380-2222","remark":"其他說明"}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章