ssm整合swagger的简单使用:
1.添加maven依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
2.编写swagger的配置类
package com.dooool.wms.config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author SunHaidong
* @date 2020/10/12 9:29
*/
@Configuration
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.build()
.apiInfo(apiInfo());
}
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("swagger 测试")
.description("接口描述")
.version("1.0.0")
.build();
}
}
3.在Controller类每一个方法中增加swagger配置
@RestController
@RequestMapping("/sysdahua")
@Api(tags = {"大华测试"},value = "TestDaHua") //配置这个注解
public class SysDaHuaController {
@Autowired
private SysDaHuaServiceImpl sysDaHuaServiceImpl;
@RequestMapping(value = "/selectByPrimary.action")
@ApiOperation(value = "信息查询",notes = "这是注释", httpMethod = "POST") //配置这个注解
public Response selectByPrimary(@RequestBody SysDaHuaRequest sysDaHuaRequest) {
}
4.在springMVC.xml文件中添加
<!-- 向容器自动注入配置 -->
<context:annotation-config/>
<!-- 将SwaggerConfig配置类注入配置类所在的路径 -->
<bean class="com.ssm.config.SwaggerConfig"/>
<!-- 配置swagger资源不被拦截 -->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
5.在web.xml 中加载springMVC.xml文件
<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:spring/springMvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
<url-pattern>/swagger/*</url-pattern>
<url-pattern>/api-docs</url-pattern>
</servlet-mapping>
以上配置完成之后,直接启动项目,访问地址:localhost:8080/swagger-ui.html 即可访问到接口界面。
6.配置项目启动自动打开接口界面(自己选择是否配置)
6.1.在web.xml 文件中配置启动项目默认打开界面
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
6.2.在webapp目录下新建index.jsp文件,并配置
注意:url=http://+访问ip+访问端口/+项目启动时访问的初始路径/swagger/swagger-ui.html
<html>
<head>
<meta http-equiv="refresh" content="0;url=http://localhost:8080/mswms/swagger/swagger-ui.html">
</head>
<body>
<div class="root">
Helloworld
</div>
</body>
</html>