Spring Boot 2 集成 Swagger


本文測試代碼使用 Spring Boot 2.1.6.RELEASE + Swagger 2.9.2

添加依賴

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

添加配置

swagger:
  enable: true

添加配置類

@Configuration
@EnableSwagger2 // 啟用 Swagger
@ConditionalOnExpression("${swagger.enable:true}") // 根據配置決定是否最終啟用 Swagger
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.karonda.springbootswagger.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SWAGGER 測試平台")
                .description("測試 SWAGGER 用")
                .termsOfServiceUrl("https://github.com/VictorBu")
                .version("1.0.0")
                .build();
    }
}

添加測試接口

@RestController
@Api(tags = "測試接口")
public class HiController {

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    @ApiOperation(value="打招呼")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firstName", value = "名字", paramType = "query", required = true),
            @ApiImplicitParam(name = "lastName", value = "姓氏", paramType = "query", required = true)
    })
    public String sayHi(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName){
        return "Hi, " + firstName + " " + lastName;
    }
}

測試

啟動程序輸入 http://localhost:8080/swagger-ui.html 即可看到效果

注意事項

如果在項目中使用了 Spring Security 則需要添加如下配置

http.authorizeRequests()
        .antMatchers(
                // -- swagger ui
                "/v2/api-docs",
                "/swagger-resources",
                "/swagger-resources/**",
                "/configuration/ui",
                "/configuration/security",
                "/swagger-ui.html",
                "/webjars/**").permitAll()
        .anyRequest().authenticated();

完整代碼:GitHub

—- 2020-06-10 更新開始 —-

最近使用中發現會報錯:

No mapping for GET /swagger-ui.html

應該是 Spring Boot 版本問題,修復:

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}

參考:swagger報錯No handler found for GET /swagger-ui.html

—- 2020-06-10 更新結束 —-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM