從嘗試著在ssm項目中配置swagger的過程中毯侦,瘋狂百度、谷歌遗增,仍然配置的有問題叫惊,好尷尬。
- 趁著熱乎著蔗坯,趕緊把配置的過程先記錄一下,再進行下一步配置和試驗:
pom.xml
<!-- swagger -->
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
<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>
web.xml
<!-- 配置spring mvc 核心Servlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:system/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 后面這個是新加的-->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/api-docs</url-pattern>
</servlet-mapping>
SwaggerConfig.java
import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@EnableSwagger
@Configuration
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(
"ssm API 測試",
"API測試",
"My Apps API terms of service",
"ne_lucifer@163.com",
"web app",
"My Apps API License URL");
return apiInfo;
}
}
spring-mvc.xml
<!-- 將 springSwaggerConfig加載到spring容器 -->
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
<!-- 將自定義的swagger配置類加載到spring容器 -->
<bean class="上面這個SwaggerConfig.java文件的路徑" />
這塊:這個是swagger-ui燎含;復制dist文件夾下面的東西拷貝到項目中
https://github.com/swagger-api/swagger-ui.git
修改:index.html里面的
換成自己的項目路徑+/api-docs
總是出現(xiàn):
-
Failed to load spec. 這個錯誤宾濒,處理方法:把swagger-ui的3.*版本換成
v2.0.24版本就變成錯誤2 了;
-
Can’t read swagger JSON from屏箍。绘梦。。赴魁。卸奉。
在web.xml中添加紅框中的,就成了開文那張圖了颖御,算是把幾個controller顯示出來了榄棵。。潘拱。