SpringBoot整合Swagger2

1.pom.xml導入依賴:html

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

 

2.添加配置類SwaggerConfig:spring

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.system.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("springboot利用swagger構建api文檔")
                .description("簡單優雅的swagger風格")
                .termsOfServiceUrl("http://")
                .version("1.0")
                .build();
    }
}

 

3. Controller層api

@RestController
@Api(description = "登陸接口")
public class LoginController {
    @ApiOperation(value = "用戶登陸" ,  notes="登陸")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "password",dataType = "String",paramType = "query"),
    })
    @PostMapping(value = "/login")
    public void login(String username,String password){
        System.err.println(username+"  "+password);
    }
}

 

4.訪問 http://localhost:8080/swagger-ui.htmlspringboot

相關文章
相關標籤/搜索