Spring-MVC + Json遇到的无法返回前端的坑
来源:互联网 发布:同花顺筹码编程公式 编辑:程序博客网 时间:2024/06/14 18:20
由于做项目使用Spring-MVC架构,而Json格式的数据又是返回前端时经常用到的,越是常见的东西往往莫名其妙的出错,后端是这样写的:
@RequestMapping(value ="/application/applicationPublish", method = RequestMethod.POST, produces = "text/html;charset=utf-8")@ResponseBodypublic Json applicationPublish(@RequestParam("AppImg") MultipartFile AppImg,@RequestParam("AppDoc") MultipartFile AppDoc, DeveloperApplication developerApplication,HttpServletRequest request, HttpServletResponse response) {SessionInfo sessionInfo = (SessionInfo) Webutil.getSessionInfoAttribute(request);//应用发布之前重设协议名称developPackageService.saveNewProtocolName(sessionInfo, developerApplication.getId());Json json = applicationService.savePublishApplication(sessionInfo, AppImg, AppDoc, developerApplication.getId(), developerApplication);return Json;}
没想到报错了,先是后台报Could not find acceptable representation,把produses去掉以后,前台又报Unexpected token < in JSON at position 0,最后查看http协议头终于解决问题:
普通的form提交后,请求头是这样的:
Accept:
application/json, text/javascript, */*; q=0.01
Content-Type:
application/x-www-form-urlencoded; charset=UTF-8
但是带文件上传的form中,请求头是这样的:
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,i
Content-Type:
multipart/form-data; boundary=----WebKitFormBounda
其中,Accept代表发送端(客户端)希望接受的数据类型。 Content-Type代表客户端希望接受的数据类型是xml类型。这说明,文件上传的form,客户端希望接收的是text/html类型,而不是Json对象,所以直接返回是有问题的。
解决上述问题:
@RequestMapping(value ="/application/applicationPublish", method = RequestMethod.POST, produces = "text/html;charset=utf-8")@ResponseBodypublic void applicationPublish(@RequestParam("AppImg") MultipartFile AppImg,@RequestParam("AppDoc") MultipartFile AppDoc, DeveloperApplication developerApplication,HttpServletRequest request, HttpServletResponse response) {SessionInfo sessionInfo = (SessionInfo) Webutil.getSessionInfoAttribute(request);//应用发布之前重设协议名称developPackageService.saveNewProtocolName(sessionInfo, developerApplication.getId());Json json = applicationService.savePublishApplication(sessionInfo, AppImg, AppDoc, developerApplication.getId(), developerApplication);BaseResult.writeJson(request, response, json);}其实就是把Json对象转化成Json字符串再传到前端即可解决。
阅读全文
1 0
- Spring-MVC + Json遇到的无法返回前端的坑
- Spring-mvc json 返回前端
- Spring MVC返回json数据的方法
- Spring MVC的@ResponseBody返回JSON串
- Spring MVC返回json数据的方法
- Spring MVC的@ResponseBody返回JSON串
- Spring MVC的@ResponseBody返回JSON串
- Spring MVC的@ResponseBody返回JSON串
- spring mvc 返回json的配置
- spring mvc返回json 类型的 数据
- spring mvc 返回json字符串的配置
- Spring MVC返回JSON格式的数据
- Spring MVC返回JSON格式的数据
- spring mvc 返回的json去掉null
- spring mvc返回json字符串的方式
- spring mvc 返回json的配置
- Spring mvc 将请求后缀改成.htm 后ajax无法返回json的问题
- MVC Json 的返回
- 操作系统的基本概念
- treelist里面的toolTipController
- 个人公众微信号 “拂晓北邮” 已经开通
- rest API 里面GET方式不能传参数,但是可以在路径里的传参数。
- 定时任务
- Spring-MVC + Json遇到的无法返回前端的坑
- Mysql
- 4.2 Android Studio工程目录结构分析
- Bootstrap File Input introduce
- 30分钟搞定iOS自定义相机
- React-Native子组件修改父组件的几种方式,兄弟组件状态修改
- HTML <form> 标签的 method 属性
- 操作系统的特征
- Java的事务处理