配置增强版swagger,可生成文档

1.引入依赖

<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>knife4j-spring-boot-starter</artifactId>
			<!--在引用时请在maven中央仓库搜索最新版本号-->
			<version>2.0.2</version>
		</dependency>

2. Swagger2Config 配置文件配置

@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config implements WebMvcConfigurer {
    @Profile({"dev", "sit"})
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)

                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(Timestamp.class);
    }
    @Profile({"dev", "sit"})
    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("标题")
                .description("描述")
                .termsOfServiceUrl("http://本机ip:端口/zlhj_telemarketing/doc.html")
                .version("1.0")
                .build();
    }
}

项目启动,访问如下:

访问启动示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一款非常流行的Java Web开发框架,它的特点是快速、简单、灵活,使得开发者能够快速构建出高质量的应用程序。Swagger是一个流行的开放API框架,它可以帮助开发者更加容易地构建和文档化RESTful Web服务。在本文中,我们将介绍如何在Spring Boot中使用Swagger。 1. 添加Swagger依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency> ``` 2. 配置Swagger 在Spring Boot的配置类中添加以下配置: ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build(); } } ``` 其中,`@EnableSwagger2`注解启用Swagger,`Docket`对象用于配置Swagger。 3. 测试Swagger 在应用程序启动后,访问`http://localhost:8080/swagger-ui.html`即可看到Swagger的UI界面,其中包含了我们所有的API接口,可以方便地测试和调用。 4. 配置Swagger的API信息 我们可以通过在`Docket`对象上添加一些配置信息来增强Swagger的功能。例如,我们可以添加API文档的标题、描述、本等信息: ``` @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot REST API") .description("Example REST API using Spring Boot and Swagger") .version("1.0.0") .build(); } ``` 5. 配置Swagger的安全认证 如果我们的API需要进行安全认证,我们可以在`Docket`对象上添加一些安全认证的配置信息。例如,我们可以添加基于Token的认证: ``` @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build() .securitySchemes(Arrays.asList(apiKey())) .securityContexts(Arrays.asList(securityContext())) .apiInfo(apiInfo()); } private ApiKey apiKey() { return new ApiKey("Token", "token", "header"); } private SecurityContext securityContext() { return SecurityContext.builder() .securityReferences(defaultAuth()) .forPaths(PathSelectors.any()) .build(); } List<SecurityReference> defaultAuth() { AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; return Arrays.asList( new SecurityReference("Token", authorizationScopes)); } ``` 其中,`apiKey()`方法用于定义Token的名称、参数名和位置,`securityContext()`方法用于定义安全上下文,`defaultAuth()`方法用于定义安全引用。这些配置信息将被用于生成Swagger的安全文档。 总结 通过以上步骤,我们可以很容易地在Spring Boot中集成Swagger,使得我们的API文档更加易读、易用、易维护。当然,Swagger还有很多其他的功能和配置选项,读者可以根据自己的实际需求进行进一步的学习和探索。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值