springboot配置swagger2

springboot+spring security配置swagger2html

這裏springboot整合springsecurity就不說了,上篇文章就有:https://www.cnblogs.com/qiantao/p/14605154.htmlweb

springboot版本:2.3.七、swagger2版本:2.9.2spring

一、pom.xml文件中添加相關依賴api

<!-- SpringSecurity 安 全 框 架 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- swagger2 接 口 文 檔 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- swagger2 UI -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

二、目錄springboot

三、SwaggerConfig配置類app

/**
 * @Author qt
 * @Date 2021/4/8
 * @Description Swagger2接口配置
 */
@Configuration
@EnableSwagger2// 該註解開啓Swagger2的自動配置
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                //api掃描的Controller包名
          .apis(RequestHandlerSelectors.basePackage("com.qt.springfashionsys.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API接口文檔")//標題
                .description("服裝管理系統")//描述
                .termsOfServiceUrl("qt我的demo")//服務的團隊
                .contact(new Contact("qt", "blog.csdn.net", "xxxxxxxx@163.com"))//做者的信息
                .license("The Apache License")//受權信息
                .licenseUrl("http://www.baidu.com")//受權的url
                .version("1.0.0")//版本號
                .build();
    }
}

四、swagger在Controller接口中添加文檔說明ide

 /**
 * @Author qt
 * @Date 2021/3/25
 * @Description
 */
@Api(value = "服裝管理業務接口", tags = { "服裝管理業務接口" }, hidden = true)
@Controller
@RequestMapping("/user")
public class UserInfoController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private UserInfoService userInfoService;

     /**
      * 根據用戶名獲取用戶信息
      * @param username 用戶名
      * @return 用戶信息
      */
    @ApiOperation(value = "XXX接口描述")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "username", defaultValue = "user", value = "用戶名", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/getUserInfo")
    @ResponseBody
    public User getUserInfo(@RequestParam String username){
        return userInfoService.getUserInfoByUsername(username);
    }
}

五、啓動項目,訪問 localhost:8080/springfashionsys/swagger-ui.html 頁面直接跳轉到以下頁面spring-boot

 接口文檔頁面:ui

 六、配置不攔截swaggerurl

若是項目加了過濾器或攔截了swagger就沒法訪問到,可能會報404,也可能報下圖錯誤,因此須要配置不攔截swagger,這裏項目中整合了security可能有些不一樣,相關文件位置能夠看我上篇文章springboot整合springsecurity。

 

  6.1 在WebSecurityConfg配置類中添加:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
            //配置靜態文件不須要認證
            .antMatchers("/static/**")
            //配置swagger2不須要認證
            .antMatchers("/v2/api-docs",
                         "/configuration/security",
                         "swagger/**",
                         "/swagger-resources",
                         "/swagger-resources/**",
                         "/swagger-ui.html",
                         "/swagger-ui.html/*",
                         "/webjars/**");
}        

  6.2 WebMvcConfig配置類:

/**
 * @Author qt
 * @Date 2021/3/19
 * @Description
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 配置靜態資源
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //配置靜態文件不須要認證,解決靜態資源被攔截的問題
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        //配置swagger2不須要認證
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}

demo地址:http://www.qnto.top/springfashionsys/swagger-ui.html

總結:實踐是檢驗真理的惟一標準。

相關文章
相關標籤/搜索