栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot利用SwaggerUI进行后端测试(knife4j接口文档工具)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot利用SwaggerUI进行后端测试(knife4j接口文档工具)

SwaggerConfig配置类:
package com.qingge.springboot.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig extends WebMvcConfigurationSupport {
    @Bean
    public Docket createRestApi(){
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
  
                // 这里指定Controller扫描包路径              
.apis(RequestHandlerSelectors.basePackage("com.qingge.springboot.controller"))
                .paths(PathSelectors.any())
                .build().ignoredParameterTypes(HttpSession.class, HttpServletRequest.class, HttpServletResponse.class);
//                .build();
    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("","","");
        return new ApiInfoBuilder()
                .title("")
                .description("")
                .version("")
                .contact(contact)
                .build();
    }
    
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

 pom.xml依赖:


    io.springfox
    springfox-swagger2
    2.9.2
    
        
            io.swagger
            swagger-models
        
    


    io.swagger
    swagger-models
    1.5.21



        
            com.github.xiaoymin
            knife4j-spring-boot-starter
            2.0.4
        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/996703.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号