From 2cf36da82d970d3a0ec408fe8a685874ec2ec7e1 Mon Sep 17 00:00:00 2001 From: ljh01459796 Date: Wed, 21 Jun 2023 19:03:54 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=20=E4=BF=AE=E5=A4=8D=20swagger=20api=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=20base64=20=E5=90=8E=20knife4j=20=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/config/OrionWebAutoConfiguration.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java index 3106416f..ff9ce2c6 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java @@ -16,6 +16,7 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; import org.springframework.http.converter.ByteArrayHttpMessageConverter; +import org.springframework.http.converter.HttpMessageConverter; import org.springframework.util.AntPathMatcher; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.cors.CorsConfiguration; @@ -25,6 +26,7 @@ import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import javax.servlet.Filter; +import java.util.ArrayList; import java.util.List; /** @@ -71,8 +73,8 @@ public class OrionWebAutoConfiguration implements WebMvcConfigurer { */ @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { - // 转换器 - FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); + // json 转换器 + FastJsonHttpMessageConverter jsonConverter = new FastJsonHttpMessageConverter(); // 配置 FastJsonConfig config = new FastJsonConfig(); // 支持的类型 @@ -84,7 +86,7 @@ public class OrionWebAutoConfiguration implements WebMvcConfigurer { MediaType.TEXT_HTML, MediaType.TEXT_XML ); - converter.setSupportedMediaTypes(mediaTypes); + jsonConverter.setSupportedMediaTypes(mediaTypes); // 序列化配置 config.setSerializerFeatures( SerializerFeature.DisableCircularReferenceDetect, @@ -92,8 +94,18 @@ public class OrionWebAutoConfiguration implements WebMvcConfigurer { SerializerFeature.WriteNullListAsEmpty, SerializerFeature.IgnoreNonFieldGetter ); - converter.setFastJsonConfig(config); - return new HttpMessageConverters(converter); + jsonConverter.setFastJsonConfig(config); + // 先获取默认转换器 + List> defaultConverters = new HttpMessageConverters().getConverters(); + List> newConverters = new ArrayList<>(); + // 将 byte converter 添加至首位 - fix swagger api 返回base64报错 + newConverters.add(new ByteArrayHttpMessageConverter()); + // 添加自定义 converter - using WrapperResultHandler + newConverters.add(jsonConverter); + // 添加默认 converter + newConverters.addAll(defaultConverters); + // 设置不添加默认 converter + return new HttpMessageConverters(false, newConverters); } /**