Java通过HttpServletRequest获取post请求中的body内容

一、使用jemeter进行接口测试时无法成功,无法获取到body中的json参数,后来通过postman测试却可以用。

1、Jemeter中content-type:application/x-www-form-urlencoded

      默认的。如果不指定content-type,默认使用此格式。

2、jemeter中传输的格式是以json进行传输,故需要改变为:content-type:application/json

参数为json格式 

{

 "key1":"value1",

 "key2":"value2"

}

3、在postman和火狐中的测试工具默认用的是:content-type:text/plain,故此在这两种工具上可以使用。

    /**
     * @ClassDes:描述信息(通过HttpServletRequest获取请求中的body参数):
     * @author guodelong
     * @time 2018年7月27日
     * @package com.tass.controller
     * @class SpringAopController
     * @Method:getUser
     */

    @RequestMapping(value = "/getUser/",method=RequestMethod.POST)
    public String getUser(HttpServletRequest request, HttpServletResponse response, BufferedReader br) throws Exception {
        // Header部分
        System.out.print(request.getHeaderNames());
        Enumeration<?> enum1 = request.getHeaderNames();
        while (enum1.hasMoreElements()) {
            String key = (String) enum1.nextElement();
            String value = request.getHeader(key);
            System.out.println(key + "\t" + value);
        }
        
        //body部分
        String inputLine;
        String str = "";
        try {
        while ((inputLine = br.readLine()) != null) {
        str += inputLine;
        }
        br.close();
        } catch (IOException e) {
        System.out.println("IOException: " + e);
        }
        System.out.println("str:" + str);

        return "接收成功!";
    }


版权声明:本文为qq_36318234原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。