xml 配置的spring+springMVC 中增加swagg
2017-11-11 本文已影响43人
程序员七平
从尝试着在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里面的
index.html
换成自己的项目路径+/api-docs
总是出现:
-
Failed to load spec. 这个错误,处理方法:把swagger-ui的3.*版本换成
image.png
v2.0.24版本就变成错误2 了;
image.png
-
Can’t read swagger JSON from。。。。。
在web.xml中添加红框中的,就成了开文那张图了,算是把几个controller显示出来了。。。
image.png