HasorDataWay接口接入到SpringDoc或openAPI3或knife4j4.0

如上,下半年计划把项目升级为springboot3,我一直想把APIdoc给换一下,之前是使用了springfox,发现好几年前已经不维护了,所以想升级,这两天就忙这个事情,其实升级挺顺利的,通过spingfox直接升级为springdoc还行,没有多少坑,主要IDEA帮助我了很多,一键替换,简单方便,这块我就不写了,比较简单;这次升级采用的是knife4j-v4.0,很不错,由于我目前版本是springboot2.7.7还是不是3.0,所以我使用了

<dependency>

<groupId>com.github.xiaoymin</groupId>

<artifactId>knife4j-openapi3-spring-boot-starter</artifactId>

<version>4.0.0</version>

</dependency>

可以到官网看看,,很不错的 传送门:https://doc.xiaominfo.com/

升级好了,没啥问题,项目接口都正常,部署到开发和测试环境都没什么问题;但是hasorDataway有问题了,因为我之前使用了swagger2进行集成了Dataway是没问题的,但是换成springDoc或者是OpenAPI-V3后,不行了,我这边也看了knife4j和hasor也没有相关升级介绍;没办法还是要升级,我也没有了解springdoc细节,但是研发这边需要看到swagger,比较急,想办法,研究了下,interface-ui/api/docs/swagger2.json 这是获取datawayswaggerjson数据,也就是说,我只需要想办法把json返回给前端进行渲染即可,我这边也粗略的看了下springdoc源码,每次请求的路径在这里

就是这个,其实我之前想重写一些,但是想想算了,后期springdoc应该有扩展点,可能我现在没有发现在哪里,我这边通过过滤器进行处理,我拦截,我配置的dataway路径

ps:后续有好的整合方案,会及时更新;目前这样先临时解决

拦截后进行写到前端即可

付源代码:

@Slf4j
@Configuration
public class CustomConfig {

    @Bean
    public OpenAPI springShopOpenAPI() {
        Contact contact = new Contact();
        contact.setEmail("zhaosy@xxx.com");
        contact.setName("zhaosy");
        contact.setUrl("http://ip+port/doc.html");
        OpenAPI openAPI = new OpenAPI()
                .info(new Info().title("demo API")
                        .description("这是一个测试项目")
                        .contact(contact)
                        .version("v1.0.0")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("SpringShop Wiki Documentation")
                        .url("https://springshop.wiki.github.org/docs"));

        return openAPI;

    }


    /**
     * openAPI过滤器,处理Dataway-swagger
     * @return
     */
    @Bean
    public FilterRegistrationBean<Filter> baseFilter(){
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new OpenAPiCustomFilter());
        filterRegistrationBean.setUrlPatterns(List.of("/v3/api-docs/dataWay"));
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }


    class OpenAPiCustomFilter implements Filter{
        @SneakyThrows
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletRequest requestFacade = (HttpServletRequest) servletRequest;
            String s = requestFacade.getRequestURL().toString();
            log.info("------------------------------{}------------------------------------------------------------",s);
            //url必须以dataWay结尾
            if (StringUtils.endsWith(s,"dataWay")){

                String s1 = HttpUtil.get("http://localhost:" + requestFacade.getServerPort() + "/interface-ui/api/docs/swagger2.json");
                //String s1 = HttpUtil.get("http://localhost:11113/interface-ui/api/docs/swagger2.json");
                log.info(s1);

                 //直接返回结果
                returnJson(servletResponse,s1);
                //终止进行后续操作
                //filterChain.doFilter(servletRequest, servletResponse);
            }else {
                //放行
                filterChain.doFilter(servletRequest, servletResponse);
            }
        }

        private void returnJson(ServletResponse response, String json) throws Exception{
            PrintWriter writer = null;
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            try {
                writer = response.getWriter();
                writer.print(json);

            } catch (IOException e) {
                log.error("response error",e);
            } finally {
                if (writer != null) {
                    writer.close();
                }
            }
        }

    }



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值