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 "接收成功!";
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值