说在前面: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版本时会报找不到页面错误。有知道的小伙伴可以在评论区留言啊