1. 程式人生 > 其它 >Excel匯出 POI 響應頭設定 Content-Type: application/vnd.ms-excel 異常解決方法

Excel匯出 POI 響應頭設定 Content-Type: application/vnd.ms-excel 異常解決方法

轉自: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; } }