说在前面:swagger版本用的是2.9.2 使用的springfox 可以直接根据代码来生成接口文档

在pom文件中引入依赖:

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

然后写一个测试控制器来测试一下:

@RestController

@RequestMapping("/people")
public class DemoController {
    @GetMapping("/hello")
    public String test(Long id,String name){
        return name;
    }
}

在启动类中加入注解EnableSwagger2:

@SpringBootApplication
@EnableSwagger2
public class DemoApplication {

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

}

这样就行了吗?
当然没有

会报以下错误:

Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException

解决的办法就是在yml文件中添加

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

输入网址访问即可:访问swagger-ui的地址
这里只测试了2.9.2版本 ,在测试3.0版本时会报找不到页面错误。有知道的小伙伴可以在评论区留言啊

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐