解决spring mvc 返回json对象时ie浏览器访问会出现下载文件弹出框

2018-03-10 13:33:23
1423次阅读
0个评论

修改spring mvc的xml配置文件

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

        <property name="messageConverters">

            <list>

                <ref bean="mappingJackson2HttpMessageConverter" />

            </list>

        </property>

    </bean>

    <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

        <property name="supportedMediaTypes">

            <list>

                <value>text/html;charset=UTF-8</value>

                <value>text/json;charset=UTF-8</value>

                <value>application/json;charset=UTF-8</value>

            </list>

        </property>

    </bean>


Spring boot 配置方法

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 
/** 
* 解决低版本ieresponsebody返回json的时候提示下载问题 
* @return 
*/ 
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() { 
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); 
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>(); 
MediaType media = new MediaType(MediaType.TEXT_HTML, Charset.forName("UTF-8")); 
supportedMediaTypes.add(media); 
jsonConverter.setSupportedMediaTypes(supportedMediaTypes); 
return jsonConverter; 
} 
@Override 
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
converters.add(customJackson2HttpMessageConverter()); 
} 


}
收藏00

登录 后评论。没有帐号? 注册 一个。