Springboot 使用swagger

本文档展示了如何在Spring Boot应用中集成Swagger2,通过添加`swagger-spring-boot-starter`依赖,配置`spring.mvc.pathmatch.matching-strategy`为`ant_path_matcher`解决启动错误,并在启动类中指定扫描包,以及创建带有API注解的Controller。通过这些步骤,可以实现API的自动化文档生成和管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、pom

<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>

2、配置文件中加入一行

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

 如果不加入,运行会报错

Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null

 

3、启动类

@SpringBootApplication(scanBasePackages = {"需要扫描的包","com.spring4all.swagger"})
public class SwaggerMpApplication {

    public static void main(String[] args) {
        SpringApplication.run(SwaggerMpApplication.class, args);
    }

}

4、controller

@RestController
public class HelloController {
    @ApiOperation("这是hello接口")
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}

5、运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值