• Excel导出 POI 响应头设置 ContentType: application/vnd.msexcel 异常解决方法


    转自:https://www.10qianwan.com/articledetail/628904.html

    先看看报的异常,大概意思是没有转换器。内含内含预设的内容类型’application/vnd.ms-excel;charset=UTF-8’]

    主要是这一行代码 supportedMediaTypes.add(MediaType.ALL);

    package tpzc.work.web;
    
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import tpzc.work.interceptor.LoginInterceptor;
    
    import java.nio.charset.Charset;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author liutao
     * @date 2020/1/16 14:24
     */
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*"). //允许跨域的域名,可以用*表示允许任何域名使用
                    allowedMethods("*"). //允许任何方法(post、get等)
                    allowedHeaders("*"). //允许任何请求头
                    allowCredentials(true). //带上cookie信息
                    exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
        }
    
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            WebMvcConfigurer.super.addInterceptors(registry);
            registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");
    
        }
    
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            //调用父类的配置
            WebMvcConfigurer.super.configureMessageConverters(converters);
            //创建FastJson的消息转换器
            FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
            //创建FastJson的配置对象
            FastJsonConfig config = new FastJsonConfig();
            //对Json数据进行格式化
            config.setSerializerFeatures(SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero,
                    SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteNullBooleanAsFalse,
                    SerializerFeature.WriteMapNullValue,
                    //禁止循环引用
                    SerializerFeature.DisableCircularReferenceDetect);
            config.setDateFormat("yyyy-MM-dd HH:mm:ss");
            config.setCharset(Charset.forName("UTF-8"));
            convert.setFastJsonConfig(config);
            convert.setSupportedMediaTypes(getSupportedMediaTypes());
            converters.add(convert);
        }
    
        public List<MediaType> getSupportedMediaTypes() {
            //创建fastJson消息转换器
            List<MediaType> supportedMediaTypes = new ArrayList<>();
            supportedMediaTypes.add(MediaType.APPLICATION_JSON);
            supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
            supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
            supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
            supportedMediaTypes.add(MediaType.APPLICATION_PDF);
            supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
            supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
            supportedMediaTypes.add(MediaType.APPLICATION_XML);
            supportedMediaTypes.add(MediaType.IMAGE_GIF);
            supportedMediaTypes.add(MediaType.IMAGE_JPEG);
            supportedMediaTypes.add(MediaType.IMAGE_PNG);
            supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
            supportedMediaTypes.add(MediaType.TEXT_HTML);
            supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
            supportedMediaTypes.add(MediaType.TEXT_PLAIN);
            supportedMediaTypes.add(MediaType.TEXT_XML);
            supportedMediaTypes.add(MediaType.ALL);
            return supportedMediaTypes;
        }
    
    }
  • 相关阅读:
    linux read的用法[转]
    1>/dev/null 2>&1的含义
    文件的权限
    【原创】server 10.192.242.184 not responding, still trying
    git使用中遇到的错误及解决方法
    linux中mmu作用的简单总结(未完)
    python版本设置
    【转】buntu TELNET服务安装配置
    【转】进程上下文和中断上下文、原子上下文的区别
    【转】【Linux】理解bitops中的__set_bit及其应用
  • 原文地址:https://www.cnblogs.com/person008/p/16044100.html
Copyright © 2020-2023  润新知