記錄使用 springboot 框架 重構spring(4.3.8版本)專案 遇到的問題
阿新 • • 發佈:2022-03-08
spring 4.3.8 對應的springboot版本為 1.4.6
有時間整理。。。。。。
springboot 上傳檔案, 前端傳檔案時,後端使用MultipartHttpServletRequest 接收,發現檔案為null:
MultiValueMap<String, MultipartFile> multiFileMap = multipartRequest.getMultiFileMap();
專案中對於訊息轉換器的配置如下:
@Bean(name = "jsonHttpMessageConverter") public MappingJackson2HttpMessageConverter jsonHttpMessageConverter() { MappingJackson2HttpMessageConverter converter= new MappingJackson2HttpMessageConverter(); converter.setPrefixJson(false); List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); mediaTypes.add(MediaType.valueOf("text/json;charset=UTF-8")); converter.setSupportedMediaTypes(mediaTypes);return converter; } @Bean public AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter( @Qualifier("jsonHttpMessageConverter") MappingJackson2HttpMessageConverter jsonHttpMessageConverter) { AnnotationMethodHandlerAdapter handlerAdapter = new AnnotationMethodHandlerAdapter(); MappingJackson2HttpMessageConverter[] converters= new MappingJackson2HttpMessageConverter[]{jsonHttpMessageConverter}; handlerAdapter.setMessageConverters(converters); return handlerAdapter; } @Bean public CommonsMultipartResolver multipartResolver() { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setDefaultEncoding("UTF-8"); return resolver; }
我的問題解決方案:啟動類上 排除springboot 對於轉換器的自動配置
@SpringBootApplication(exclude={MultipartAutoConfiguration.class})