SpringBoot如何引入Swagger 在线可视化接口文档。

1.swagger介绍:

现在项目都是前后端分离,随着接口越来越多导致接口文档不及时更新或者压根没有接口文档,导致联调的成本增加,Swagger 是为了解决这个问题。

Swagger 是一个规范和完整的框架,用于生成对应的API文档的描述,还能提供在线接口测试的功能。

2.swagger引入:

pom文件引入

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

swagger配置类创建

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()                                //根据自己controller修改对应包地址
                .apis(RequestHandlerSelectors.basePackage("com.example.demo2.controller"))
                .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档API") //设置文档的标题
                .description("Service API 接口文档") // 设置文档的描述
                .version("1.0") // 设置文档的版本信息-> 1.0.0 Version information
                .build();
    }
}

3.swagger引入时出现的问题

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.17.jar:5.3.17]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.17.jar:5.3.17]
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.17.jar:5.3.17]
	at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_181]
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.17.jar:5.3.17]
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.17.jar:5.3.17]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.17.jar:5.3.17]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.17.jar:5.3.17]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.5.jar:2.6.5]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) [spring-boot-2.6.5.jar:2.6.5]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) [spring-boot-2.6.5.jar:2.6.5]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-2.6.5.jar:2.6.5]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) [spring-boot-2.6.5.jar:2.6.5]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) [spring-boot-2.6.5.jar:2.6.5]
	at com.example.demo2.Demo2Application.main(Demo2Application.java:10) [classes/:na]

springboot的版本太高了,修改springboot版本为2.5.6或者修改swagger的版本为2.9.2。

4.引入knife4j前端UI

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.8</version>
</dependency>

启动项目访问项目地址加doc.html即可。例子http://127.0.0.1:8080/doc.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答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接口。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值