@RequestBody 注解

@RequestBody 注解

作用:用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应
的数据绑定到要返回的对象上。再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上

GET、POST方式提时, 根据request header Content-Type的值来判断:

1、 application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
2、 multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
3、其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);

写代码时遇到 错误


Could not parse multipart servlet request; nested … rejected because no multipart boundary was found

原因是 ajax提交 请求的 content-type ,为contentType:"multipart/form-data",

@RequestMapping(value="/importcustomer", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        @ResponseBody
        public Result<Integer> importCustomer(@RequestPart(name="customerExcel",required=false) MultipartFile
                customerExcel,@RequestParam Integer Id,@RequestParam Integer teamname){

@ResponseBody 注解

作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机: 返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

猜你喜欢

转载自www.cnblogs.com/gloria-liu/p/9088646.html
今日推荐