Swagger集成

1、引入依赖

        <!-- Swagger API文档 -->
        <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、加入配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author liuyf
 * @description:
 * @date: 2021/12/30 16:36
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 自行修改为自己的包路径
                .apis(RequestHandlerSelectors.basePackage("com.jij.uap"))
                .paths(PathSelectors.any())
                .build();
    }

//    /**
//     * swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
//     *
//     * @return Docket
//     */
//    @Bean(value = "defaultApi2")
//    public Docket defaultApi2() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .select()
//                //此包路径下的类,才生成接口文档
//                .apis(RequestHandlerSelectors.basePackage("org.jeecg"))
//                //加了ApiOperation注解的类,才生成接口文档
//                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                .paths(PathSelectors.any())
//                .build();
//        //.globalOperationParameters(setHeaderToken());
//    }

    /**
     * api文档的详细信息函数,注意这里的注解引用的是哪个
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // //大标题
                .title("基建三期")
                // 版本号
                .version("1.0")
//          .termsOfServiceUrl("NO terms of service")
                // 描述
                .description("后台API接口")
                .build();
    }

    /**
     *
     * 显示swagger-ui.html文档展示页,还必须注入swagger资源:
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

至此,Swagger原生UI已经可以通过 IP:PORT/swagger-ui.html#/ 进行访问。

3、UI及汉化

3.1引入依赖
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-ui</artifactId>
            <version>2.0.8</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.8</version>
        </dependency>
3.2启动并访问

默认使用 IP:PORT/doc.html#/home访问

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot集成Swagger是为了更方便地生成API文档,使得API文档更加规范、易读和易于维护。 Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。Spring Boot是一个快速开发框架,集成了大量的开箱即用的功能,能够帮助开发者快速地构建应用。 在Spring Boot集成Swagger,需要引入相应的依赖,配置Swagger相关的注解和配置信息。通过访问Swagger UI页面,可以方便地查看API文档、测试API接口等。 通过Spring Boot集成Swagger,我们可以更好地管理和维护API文档,提高开发效率和代码质量。 ### 回答2: Spring Boot可以通过集成Swagger来自动生成API文档。Swagger是一个规范和工具集,用于设计、构建和维护RESTful风格的API文档。以下是集成Swagger的步骤: 1. 在pom.xml文件中添加Swagger依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.3.1</version> </dependency> ``` 2. 创建一个Swagger配置类,使用@EnableSwagger2注解启用Swagger: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } } ``` 3. 使用Swagger注解描述API接口。在Controller类或方法上添加@Api、@ApiOperation等注解来描述API的信息、请求和响应参数等。 4. 运行Spring Boot应用程序,并访问"http://localhost:8080/swagger-ui.html",可以看到自动生成的API文档页面。 集成Swagger可以方便地为API接口生成文档,并且可以在页面上进行测试。开发人员和客户端可以根据这些文档了解API的使用方式和参数要求,减少沟通成本,提高开发效率。 ### 回答3: Spring Boot是用于构建Java应用程序的开源框架,而Swagger是用于设计、构建和文档化RESTful API的工具。将Swagger集成Spring Boot项目中,可以方便地生成API文档,并提供交互式API文档浏览界面。 首先,需要在项目的pom.xml文件中添加Swagger依赖。可以使用以下代码片段添加Swagger的依赖: ```xml <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> ``` 添加完依赖之后,需要创建一个配置类来启用Swagger。可以创建一个名为SwaggerConfig的类,并使用@EnableSwagger2注解启用Swagger。 ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } } ``` 在这个配置类中,可以通过api()方法来配置Swagger的一些参数,例如扫描的包路径、API路径等。 配置完成后,可以通过访问http://localhost:8080/swagger-ui.html来查看Swagger生成的API文档。在这个界面上,可以查看每个API的详细信息,包括请求参数、响应类型等。同时,还提供了测试API的功能,方便进行接口的调试和测试。 需要注意的是,集成Swagger只是在项目中添加了API文档的功能,实际的API实现还需要编写具体的业务逻辑代码。 综上所述,通过上述步骤,我们可以将Swagger集成Spring Boot项目中,并生成具有交互性的API文档界面,方便开发和测试人员查阅和测试API接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值