1.添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
2.获取后台的json数据
/**
* swagger的配置对象
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* 把一个Docker交给spring管理
* Docket:springfox提供的文档的配置对象;
*
* @return
*/
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().build();
}
}
3.访问地址
http://localhost:8888(自定义端口)/v2/api-docs
4.使用swagger 2自带的UI
添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
配置视图
@Component
public class SwaggerMvcConfigurerAdapter implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//swagger-ui.html 可以更改为自己的UI
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
}
5.访问地址
http://localhost:8888(自定义端口)/swagger-ui.html