接口开发

接口开发最基本的是如何获取请求的参数和如何返回查到的数据,参数的格式采用JSON

以struts框架为例

post方式传递json数据参数

public class GetbusinessInformation  extends BaseAction{
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
	JSONObject jsonRequestParam = JSONObject.fromObject(this.getStrResponse(request));
	JSONObject json = new JSONObject();//返回的JSON数据
	String requst_code = (String)jsonRequestParam.get("req_code");//请求接口路由识别码
    System.out.println(jsonRequestParam.toString());//打印请求参数
    if(StringUtils.equals("001", requst_code)){
    	json = ...................//此处调用具体的实现方法
    }

    if(StringUtils.equals("002", requst_code)){
    	json = ...................//此处调用具体的实现方法
    }

    ...........................

    System.out.println(json.toString());//打印输出参数
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json.toString());
    response.getWriter().flush();
    response.getWriter().close();
    return null;
    }
}
//获取请求体中的数据  
	public String getStrResponse(HttpServletRequest request) throws IOException{  
		int contentLength = request.getContentLength();
	    if(contentLength<0){
	    	return null;
	    }
		byte buffer[] = new byte[contentLength];
		for (int i = 0; i < contentLength;) {
			int len = request.getInputStream().read(buffer, i, contentLength - i);
			if (len == -1) {
				break;
			}
			i += len;
         }
		return new String(buffer, "utf-8");
    }

get方式传递参数,可直接用request.getParameter("参数名")获取。







  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值