可參考https://blog.csdn.net/y534560449/article/details/53694443
1、pom.xml引入相應的包, 各個包的版本根據情況定
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.1</version>
</dependency>
2、創建自定義swagger類
@Configuration
@EnableSwagger
@EnableWebMvc
public class SwaggerConfig {
private SpringSwaggerConfig springSwaggerConfig;
/**
* Required to autowire SpringSwaggerConfig
*/
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
{
this.springSwaggerConfig = springSwaggerConfig;
}
/**
* Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
* framework - allowing for multiple swagger groups i.e. same code base
* multiple swagger resource listings.
*/
@Bean
public SwaggerSpringMvcPlugin customImplementation()
{
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
.includePatterns(".*?");
}
private ApiInfo apiInfo()
{
ApiInfo apiInfo = new ApiInfo(
"江湖味系統API",
"前後端接口API管理",
"",
"[email protected]",
"無",
"");
return apiInfo;
}
}
3、引入swagger頁面文件
到git上下載2.1.5版本的swagger-ui:https://github.com/swagger-api/swagger-ui/tree/v2.1.5/dist
下載後將dist文件夾全部拷貝到webapp目錄下,如下圖:
4、與spring整合
<!-- 將 springSwaggerConfig加載到spring容器 -->
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
<!-- 將自定義的swagger配置類加載到spring容器 -->
<bean class="com.taster.util.config.SwaggerConfig" />
<!-- 靜態資源文件,不會被Spring MVC攔截 -->
<mvc:resources mapping="/swagger/**" location="/WEB-INF/swagger/" />
5、配置到controller中
6、swagger常用註解介紹與使用
參考地址1:https://www.jianshu.com/p/12f4394462d5
參考地址2:https://blog.csdn.net/xupeng874395012/article/details/68946676