postman进行post请求,后台参数接收

xiaoxiao2025-11-09  6

postman中需要设置的参数入下图所示:

在controller中进行mapping配置 :  

@RequestMapping(value = "api/test",method = RequestMethod.POST) @ResponseBody public String test(HttpServletRequest request, HttpServletResponse response) { String val = request.getParameter("key");//获取post请求参数 return val; }

注意:

进行post请求时,设置[{"key":"Content-Type","value":"application/json","description":""}],只能根据inputStream来获取参数,不能直接使用request.getParameter("key")来获取参数:

方式一:使用@ResponseBody注解,定义包含所有参数的对象,直接封装到对象属性中

@RequestMapping(value = "api/test",method = RequestMethod.POST) @ResponseBody public String test(@ResponseBody Param param) { String val = param.getKey();//获取post请求参数 return val; } @Data pubilc class param{ private String key; }

方式二:在controller中通过request获取流

String s = StreamUtils.copyToString(request.getInputStream(), Charset.forName("UTF-8")); Map map = JSON.parseObject(s, Map.class);

 

将json转换为map类型,这样就是key-value形式,可以通过key来获取相应的值。

 

相关文章:

 

转载请注明原文地址: https://www.6miu.com/read-5039362.html

最新回复(0)