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