knife4j 正确开启HttpServletResponse流输出

问题描述

相信大家做 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 输出就正常了。在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值