axios 发 post 请求,后端接收不到参数的详细解决方案

版权声明:更多信息请关注 wwws.shinians.com 官网 https://blog.csdn.net/zzhuan_1/article/details/83745631

问题描述 :axios post 请求或者get请求后接收不到参数

后端接收设置: @RequestParam @RequestBody设置的原因

  由于spring的RequestParam注解接收的参数是来自于requestHeader中,即请求头,也就是在url中,格式为xxx?username=123&password=456,而RequestBody注解接收的参数则是来自于requestBody中,即请求体中。

---------------------------------------------------------------------------------------

前端用axios发送请求:Content-Type默认是

application/json;charset=UTF-8

而服务器是

'Content-Type': 'application/x-www-form-urlencoded'

原理剖析在最后

                                          后端 解决方案:

前端解决方案

https://blog.csdn.net/csdn_yudong/article/details/79668655 

一:

  如果为get请求时,后台接收参数的注解应该为RequestParam,如果为post请求时,则后台接收参数的注解就是为RequestBody。附上两个例子,截图如下:

      get请求

 @RequestMapping(value = "/anaData")
    public String selectOrderInfo(@RequestParam("method") String method, @RequestParam Map<String,Object> params) {
        LOG.debug(TextUtils.format("/***数据 分析模块,統計查詢 通用方法{0}**/", method));
        return analysisClientService.selectOrderInfo(method,params);
    }

post请求

 @PostMapping(value = "/center")
    public String requestApi(@RequestBody Map<String, Object> params){
        loger.debug(TextUtils.format("调用开始------"));
        return apiCenterClientService.requestApi(params);
    }

二、复杂参数形式

       未完待续。。。。。。。

tips:如果参数 有HttpServletRequest需要 注入可以用下面方式

  在Controller中注入了HttpServletRequest,形式如下所示:

@RestController
public class AutowiredRequestController {
 
    @Autowired
    private HttpServletRequest request;
}


不会有线程安全问题!Controller层中所注入的HttpServletReuqest的实现类为JDK动态代理生成的一个代理类,从Request中获取值的时候是从ThreadLocal中得到的对象中的值。

----------------------------------------------------------------------未处理部分-----------------------------------------

@RequestParam
用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。提交方式为get或post。(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)

RequestParam实质是将Request.getParameter() 中的Key-Value参数Map利用Spring的转化机制ConversionService配置,转化成参数接收对象或字段。

get方式中query String的值,和post方式中body data的值都会被Servlet接受到并转化到Request.getParameter()参数集中,所以@RequestParam可以获取的到

@RequestBody
处理HttpEntity传递过来的数据,一般用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据。

GET请求中,因为没有HttpEntity,所以@RequestBody并不适用。
POST请求中,通过HttpEntity传递的参数,必须要在请求头中声明数据的类型Content-Type,SpringMVC通过使用HandlerAdapter 配置的HttpMessageConverters来解析HttpEntity中的数据,然后绑定到相应的bean上。

@RequestBody用于post请求,不能用于get请求

这里涉及到使用@RequestBody接收不同的对象
1. 创建一个新的entity,将两个entity都进去。这是最简单的,但是不够“优雅”。
2. 用Map<String, Object>接受request body,自己反序列化到各个entity中。
3. 类似方法2,不过更为generic,实现自己的HandlerMethodArgumentResolver。

当前台界面使用GET或POST方式提交数据时,数据编码格式由请求头的ContentType指定。分为以下几种情况:
1. application/x-www-form-urlencoded,这种情况的数据@RequestParam、@ModelAttribute可以处理,@RequestBody也可以处理。
2. multipart/form-data,@RequestBody不能处理这种格式的数据。(form表单里面有文件上传时,必须要指定enctype属性值为multipart/form-data,意思是以二进制流的形式传输文件。)
3. application/json、application/xml等格式的数据,必须使用@RequestBody来处理。

猜你喜欢

转载自blog.csdn.net/zzhuan_1/article/details/83745631