Spring Boot中整合swagger2,访问时出现白页

1.概述

​ 前后端分离开发模式中,api文档是最好的沟通方式。Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)规范性 (并且保证接口的规范性,如接口的地址,请求方式,参数及响应格式和错误信息)一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)可测性 (直接在接口文档上进行测试,以方便理解业务)

2.配置swagger2

(1)创建子模块common,在common模块引入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <scope>provided </scope>
    </dependency>

    <!--mybatis-plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <scope>provided </scope>
    </dependency>

    <!--lombok用来简化实体类:需要安装lombok插件-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

    <!--swagger-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
    </dependency>
</dependencies>

(2)在common下创建子模块

(3)在子模块中创建swagger2配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("ggkt")
                .apiInfo(webApiInfo())
                .select()
                //只显示api路径下的页面
                //.paths(Predicates.and(PathSelectors.regex("/api/.*")))
                .build();
    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站-API文档")
                .description("本文档描述了网站微服务接口定义")
                .version("1.0")
                .contact(new Contact("atguigu", "http://atguigu.com", "atguigu.com"))
                .build();
    }
}

(4)在其他模块下引入service_utils依赖

<dependency>
    <groupId>com.atguigu</groupId>
    <artifactId>service_utils</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

(5在启动类上添加注解,进行测试

@SpringBootApplication
@ComponentScan("com.atguigu")
public class ServiceVodApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceVodApplication.class, args);
    }
}

浏览器输入固定地址 http://localhost:8301/swagger-ui.html,结果出现白页

3.问题处理

common模块的配置文件里没有指定版本号,我使用的是3.6.3版本的maven,指定swagger2版本为2.9.2,问题解决
在这里插入图片描述

  • 17
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Spring Boot可以很方便地集成Swagger,只需要添加相应的依赖和配置即可。 1. 添加Swagger依赖 在pom.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> ``` 2. 配置SwaggerSpring Boot的配置类添加Swagger的配置: ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } } ``` 其,`@EnableSwagger2`注解开启Swagger,`Docket`是Swagger的主要配置类,可以通过它来配置Swagger的各种属性,如文档的基本信息、接口扫描等。 3. 测试Swagger 启动Spring Boot应用程序后,访问http://localhost:808/swagger-ui.html即可看到Swagger的UI界面,可以在这里查看API文档、测试接口等。 以上就是Spring Boot整合Swagger的简单介绍。 ### 回答2: Spring Boot是一个基于Spring框架的快速开发工具,它提供了很多有用的功能,例如自动配置和内嵌式Web服务器。而Swagger是一个用于设计、构建和文档化Web API的工具,它可以帮助开发者快速地创建高质量的API文档。 Spring Boot整合Swagger可以让开发者更加便捷地创建API文档,而且可以帮助开发者快速地了解和理解API的使用方法。下面是Spring Boot整合Swagger的详细介绍: 1. 引入Swagger依赖 在pom.xml文件添加Swagger的依赖: ``` <!-- swagger依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox.version}</version> </dependency> <!-- springfox swagger ui,可选(用于引入UI界面,方便查看API文档) --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox.version}</version> </dependency> ``` 2. 创建Swagger配置类 创建一个Swagger配置类,用于配置Swagger的信息和API文档的生成规则。配置示例代码如下: ``` @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() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Swagger Demo API") .description("This is a sample API for testing Swagger") .version("1.0") .build(); } } ``` 3. 设置Swagger注解 在Controller类或者Controller方法上添加Swagger注解,用于指定API的相关信息和描述。例如: ``` @RestController @RequestMapping("/example") @Api(tags = "示例API") public class ExampleController { @ApiOperation("根据ID获取示例信息") @GetMapping("/{id}") public Example getById(@PathVariable Long id) { //... } @ApiOperation("创建示例信息") @PostMapping("") public Example create(@RequestBody Example example) { //... } @ApiOperation("更新示例信息") @PutMapping("/{id}") public Example update(@PathVariable Long id, @RequestBody Example example) { //... } @ApiOperation("删除示例信息") @DeleteMapping("/{id}") public void deleteById(@PathVariable Long id) { //... } } ``` 4. 访问Swagger UI界面 启动Spring Boot应用程序后,访问http://localhost:端口号/swagger-ui.html即可查看API文档。Swagger UI界面会自动根据Swagger配置类和Swagger注解生成API文档并展示出来,方便开发者查看、测试和调试API接口。 总结来说,Spring Boot整合Swagger可以方便开发者创建高质量的API文档,减少API接口文档编写的工作量,并提高API的可读性和易用性。同Swagger也提供了一些测试和调试API接口的工具,可以帮助开发者更加方便地进行API开发和测试。 ### 回答3: Spring Boot 是一个快速开发的框架,它提供了一个快速创建基于Spring的应用程序的方式。Swagger 又被称为 OpenAPI,是一种用于创建、设计、构建和文档化 RESTful API 的工具。Swagger 定义了一种描述 API 的标准集合,可以生成可与 API 交互的文档。Spring Boot 整合 Swagger 可以帮助我们更加方便地创建和文档化 RESTful API。 在 Spring Boot 整合 Swagger,我们需要引入两个依赖:springfox-swagger2 和 springfox-swagger-ui。springfox-swagger2 用于生成 Swagger 的 JSON 描述和 UI,而 springfox-swagger-ui 则用于展示 Swagger UI。我们可以通过在 pom.xml 文件添加以下依赖来引入这两个依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox.version}</version> </dependency> ``` 其 `${springfox.version}` 可以替换为具体的版本号。 添加依赖后,我们需要配置 Swagger。在 Spring Boot ,我们可以通过创建一个 Swagger 配置类来配置 Swagger。我们可以创建一个类并添加 @Configuration 和 @EnableSwagger2 注解来实现这一步骤。代码如下: ``` @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(); } } ``` 其,`@Configuration` 表示该类是一个配置类,`@EnableSwagger2` 表示启用 Swagger。`Docket` 是一个用于配置 Swagger 的 bean,我们可以通过 `.select()` 方法来指定扫描哪些包下的 controller 和 API,通过 `.paths()` 方法来指定扫描哪些 URL,这里我们使用 `.any()` 表示扫描全部 URL。 使用 Swagger,我们需要在 Controller 添加一些注解来描述 API。这里我们只介绍两个常用注解:`@ApiOperation` 和 `@ApiParam`。`@ApiOperation` 表示该方法是一个 API,可以添加一些描述信息,例如方法名、方法描述、请求参数等。`@ApiParam` 表示请求参数的描述信息。代码如下: ``` @RestController @RequestMapping("/api") public class UserController { @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息") @ApiParam(name = "id", value = "用户ID") @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { User user = new User(); user.setId(id); user.setName("Tom"); user.setAge(18); return user; } } ``` 在上面的代码,我们创建了一个 UserController,其包含一个获取用户信息的 API。`@GetMapping("/{id}")` 表示我们可以通过 GET 请求来访问该 API,并且 URL 是 `/api/{id}`。`@PathVariable Long id` 表示请求参数名为 id,类型为 Long。`@ApiOperation` 添加了方法名和方法描述信息,`@ApiParam` 添加了请求参数的描述信息。 最后,我们启动应用程序并访问 `http://localhost:port/swagger-ui.html` 即可查看 Swagger 生成的 API 文档。 总结来说,Spring Boot 整合 Swagger 可以帮助我们更加方便地创建和文档化 RESTful API,同也提供了一种简单易用的方式来生成 API 文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值