Swagger2是一款RESTFUL接口在线生成工具,对于我们开发非常方便

第一笔引入pom:

<!-- swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger2.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>mapstruct</artifactId>
            <groupId>org.mapstruct</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger2.version}</version>
</dependency>

第二步:

在application文件上加上@EnableSwagger2注解开启

第三步(可以不要):配置swagger

package com.springbootmybatisdemo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("Swagger2接口文档")
                .termsOfServiceUrl("https://blog.csdn.net/Java_Mrsun")
                .contact("李太阳")
                .version("1.0")
                .build();
    }
}

 

第四部(可不要):在接口上添加相关注释

SpringBoot集成Swagger2

 

最后网页打开  http://localhost:8082/swagger-ui.html  即可访问,如下: 

SpringBoot集成Swagger2

相关文章:

  • 2021-09-15
  • 2021-04-05
  • 2021-09-29
  • 2021-08-14
猜你喜欢
  • 2022-01-08
  • 2021-11-29
相关资源
相似解决方案