zoukankan      html  css  js  c++  java
  • WebAPI服务器收发json数据

    1. 服务端接收

    1.1 通过HttpServletRequest request获取json信息

    在这里插入图片描述

        @RequestMapping("/dice/init")
        @ResponseBody
        public String diceInit(HttpServletRequest request, Integer Count) {
    		String param= null;
    		try {
    		    BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(),
    		            "UTF-8"));//通过I0流获取data body
    		    StringBuilder responseStrBuilder = new StringBuilder();
    		    String inputStr;
    		    while ((inputStr = streamReader.readLine()) != null){
    		        responseStrBuilder.append(inputStr);
    		    }
    		    JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());
    		    param= jsonObject.toJSONString();
    		    System.out.println(param);
    		} catch (Exception e) {
    		    e.printStackTrace();
    		}
    	}
    

    1.2 使用@RequestBody方法获取

    在这里插入图片描述

    @RequestMapping("/dice/valueInit")
    @ResponseBody
    public BaseResponse valueInit(Integer Round, Integer Count, @RequestBody JSONObject obj){
       String userName = obj.getString("userName");
       String steamerName = obj.getString("steamerName");
       String diceUUID = safeUtils.shortMD5(userName+"&"+steamerName+ textUtils.getCurrentTime());
    }
    

    2. 服务端返回

    2.1 直接返回JSONObject

    在这里插入图片描述

    2.2 返回实体类

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    hdoj_1800Flying to the Mars
    SPFA模版
    树状数组
    hdoj_1385Minimum Transport Cost
    hdoj_2112
    hdoj_3665Seaside
    Java的垃圾回收之算法
    Oracle和MySQL、PostgreSQL特性对比
    什么是java对象的强、软、弱和虚引用
    线程池(java.util.concurrent.ThreadPoolExecutor)的使用(一)
  • 原文地址:https://www.cnblogs.com/litchi99/p/13504311.html
Copyright © 2011-2022 走看看