zoukankan      html  css  js  c++  java
  • springmvc中@requestBody的使用与原理

    web应用中浏览器发送http请求,传递参数有两种方式:一种是参数作为param传递的,java服务端通过request.getParameter(name)即可获取到参数内容;

    第二种是把参数放在了http的requestBody中(contentType:”application/json”),这时,使用getParameter就获取不到了,可以通过读取body获取。

    1、原生servlet获取

    http body通过读取input流可以得到

    BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
    StringBuffer sb = new StringBuffer("");
    String temp;
    while ((temp = br.readLine()) != null) { sb.append(temp); } br.close(); String params = sb.toString();

    2、springmvc获取

    SpringMVC中有一个@RequestBody 注解,就是用来将请求body中的json转换为java对象的,不仅转换普通的java bean,也可以是List

    public void token(HttpServletRequest request, HttpServletResponse response, @RequestBody String entity) throws Exception {
      JSONObject json = JSONObject.fromObject(entity);
    }

    参考链接:

    http://www.jianshu.com/p/c46efc3e9e96

  • 相关阅读:
    正则表达式之re模块
    collections模块
    openpyxl模块
    hashlib模块
    random模块
    os模块
    sys模块
    nodeType
    数据类型转换
    添加删除
  • 原文地址:https://www.cnblogs.com/dali-lyc/p/7305036.html
Copyright © 2011-2022 走看看