解决Spring Boot @Responsebody后IE浏览器返回json时提示下载问题

解决Spring boot返回JSON对象后在IE客户端会提示下载JSON文件的问题

  • Spring boot版本1.5.3.RELEASE
  • 问题详细原因:在一个通用上传下载接口中,使用@Responsebody+@RequestMapping(method=RequestMethod.POST, produces = “application/json; charset=UTF-8”),这样返回的为JSON对象

其实这样返回在除IE(又是IE)外的浏览器一般都没有问题,但是如果IE中前端使用iframe框架的话,IE就会将JSON对象当成下载文件(即使是IE11这样支持application/json的也不行),解决这个问题应该有不少办法,网上找了找都不行,最后还是自己尝试着解决,详细过程:

  1. 将@RequestMapping(method=RequestMethod.POST, produces = “application/json; charset=UTF-8”)中的produces属性改为"text/plain"普通文本形式
  2. 在spring上下文中添加一个支持text/plain的消息转换器
@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 
	/** 
	* 解决低版本ieresponsebody返回json的时候提示下载问题 
	* @return 
	*/ 
	public FastJsonHttpMessageConverter getFastJsonHttpMessageConverter() { 
		 //1.需要定义一个convert转换消息的对象;
	    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
	    //2处理ie浏览器保存数据时出现下载json数据问题
	    List<MediaType> fastMediaTypes = new ArrayList<>();
	    fastMediaTypes.add(MediaType.TEXT_PLAIN);
	    //3.在convert中添加配置信息.
	    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
	    return fastJsonHttpMessageConverter;
	} 
	@Override 
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
		converters.add(getFastJsonHttpMessageConverter()); 
	} 
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值