swagger集成以及knife4j的增強


#pom依賴

<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>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.2</version>
    <exclusions>
        <exclusion>
            <artifactId>swagger-models</artifactId>
            <groupId>io.swagger</groupId>
        </exclusion>
    </exclusions>
</dependency>

 

#配置類

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                //為當前包下controller生成API文檔
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build()
                //添加登錄認證
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false);
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("api文檔標題")
                .description("api文檔描述")
                .contact(new Contact("聯系人", "www.example.com", "郵箱地址"))
                .version("版本號")
                .build();
    }
}

 

#注解使用

controller:

@Api(tags = "系統管理")

method:

@ApiOperation("用戶管理")

entity:

@ApiModel(value = "用戶信息")
public class user{
    @ApiModelProperty(value = "用戶信息")
    private String username;
}

 

#默認訪問地址

http://${host}:${port}/doc.html


免責聲明!

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



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