问题描述
相信大家做 java 后端开发的时候,会常用一个文档管理工具 swagger,但原生的页面,布局都不是特别的合理,所以这边就可以引入一个新的增强版插件
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
因此也遇到图片流、excel 导出流无法直接有效在浏览器中输出的问题。
具体问题
@EnableKnife4j
@EnableSwagger2
@Configuration
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfigurer {
@Value("${swagger-ui.enabled:true}")
private boolean isOpen;
@Bean
public Docket createCommonApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(isOpen)
.apiInfo(apiInfo())
.protocols(new HashSet<>(Arrays.asList("https", "http")))
.select()
.apis(RequestHandlerSelectors.basePackage("com.xx.controller"))
.paths(PathSelectors.any())
.build().groupName("ALL-API");
}
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("xx技术系统-对外API文档")
.description("接口文档")
.termsOfServiceUrl("https://localhost:8087/xx/doc.html")
.contact(new Contact("xx技术有限公司", "https://www.xx.com", "xx@qq.com"))
.version("1.0.0")
.build();
}
}
注意:DocumentationType.SWAGGER_12和 DocumentationType.SWAGGER_2情况下HttpServletResponse流输出会变成乱码。
解决办法
当DocumentationType.OAS_30的时候,HttpServletResponse 输出就正常了。