基于restful服务端接口(一)

来源:互联网 发布:pokemon go unity源码 编辑:程序博客网 时间:2024/05/29 16:55

熟练使用restful接口

当我们会使用restful接口后,我们想进一步的学习restful接口。

我们都知道,用于接口传输参数一般有两种方式:XML、json。

下面就是使用json,来进行数据交互。

1)通过get的方式

    // 通过json数据,改变后台的结果    @GET    @Path("/getTestInfoItemJson")    @Produces(MediaType.APPLICATION_JSON)    public String getMassarrayInfo(@Context HttpServletRequest request,            @Context HttpServletResponse response) throws Exception {        Map<String, Object> map = new HashMap<String, Object>();        // ajax跨域请求        response.setHeader("Access-Control-Allow-Origin", "*");        response.setHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE");        response.setHeader("Access-Control-Max-Age", "3600");        try {            String jsondata = request.getParameter("data");            String result = "", MId = "", code = "";            JSONObject fromObject2 = JSONObject.fromObject(jsondata);            // 得到主表ID            MId = (String) fromObject2.get("mId");            JSONArray fromObject = fromObject2.getJSONArray("infoData");            if (fromObject != null) {                for (int i = 0; i < fromObject.size(); i++) {                    JSONObject jsonJ = fromObject.getJSONObject(i);                    // 得到json对象                    if (jsonJ.containsKey("code")) {                        code = jsonJ.getString("code");                        result = jsonJ.getString("result");                    }                     //这里用作自己的逻辑编写                }            }            map.put("result", "1");        } catch (Exception e) {            e.printStackTrace();            map.put("result", "0");        }        String jsonString = JsonUtils.toJsonString(map);        return jsonString;    }

2)通过post方式

@POST    @Path("/createTest")    @Produces(MediaType.APPLICATION_JSON)    @Consumes(MediaType.APPLICATION_JSON)    public String createSampleOrder(@Context HttpServletRequest request,            @Context HttpServletResponse response) throws Exception {        response.setHeader("Access-Control-Allow-Origin", "*");        response.setHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE");        response.setHeader("Access-Control-Max-Age", "3600");        response.setContentType("application/json;charset=utf-8");        StringBuffer str = new StringBuffer();        JSONObject json=new JSONObject();         String code = "";        try {            BufferedInputStream in = new BufferedInputStream(request.getInputStream());            int i;            char c;            while ((i = in.read()) != -1) {                c = (char) i;                str.append(c);            }            String strJson = java.net.URLDecoder.decode(str.toString(),"utf-8");            System.out.println("接收json:"+strJson);            if(str!=null){                JSONObject dataJson = JSONObject.fromObject(strJson);                JSONObject data = dataJson.getJSONObject("data");                String testDate = dataJson.getString("testDate");                System.out.println("接收data:"+data);                System.out.println("接收时间:"+testDate);                code = data.getString("testNo");//测试编码                //填写逻辑代码                json.put("error", "0");                json.put("errmsg", "");            }        } catch (Exception ex) {            ex.printStackTrace();            json.put("errno", "1");            json.put("errmsg", ex.getMessage());        }        System.out.println("返回json:"+json.toString());        //HttpPostData(code);        return json.toString();    }