项目介绍
ssm:spring、springmvc、mybatis
此项目为ssm单体项目,集成了
ssm集成swagger
什么事swagger:【不一一赘述了】
集成的注意事项:
这里是采用ssm+swagger的基础方式,且swagger为2.0以上的版本。
步骤:
- 引入依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
有的博客是引入以下的依赖,此依赖为1.0的版本,两种依赖不能同时引入,会出现意想不到的BUG。
<!-- https://mvnrepository.com/artifact/com.mangofactory/swagger-springmvc -->
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
2.下载swagger源码
下载地址: 点击下载swagger
https://github.com/swagger-api/swagger-ui/tree/v2.1.5
3.在web项目webapp下新建文件夹swagger,将下载的源码导入到文件夹下:
修改index.html页面:如图所示。
1.0版本可以简写为:”/api-docs”
2.0版本简写为:”/v2/api-docs”
4,编写swagger工具类
package com.zengqiang.utils;
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.PathSelectors;
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: iszengziqiang@163.com
* @Date: 2018/12/30 10:30
* @Version: 1.0
* @Desc: TODO
**/
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).select()
//扫描指定包中的swagger注解
//.apis(RequestHandlerSelectors.basePackage("cn.exrick.controller"))
//扫描所有有注解的api,用这种方式更灵活
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("ssm-single Api Documentation")
.description("ssm-single项目后台API接口文档")
.termsOfServiceUrl("https://www.baidu.com/")
.license("iszengziqiang@163.com")
.version("1.0.0")
.build();
}
}
5,编写API
package com.zengqiang.controller;
import com.zengqiang.student.service.StudentService;
import com.zengqiang.utils.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @Author: iszengziqiang@163.com
* @Date: 2018/12/29 14:47
* @Version: 1.0
* @Desc: TODO
**/
@Controller
@RequestMapping("/student")
@Api(value = "student的操作类", description = "学生类")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/s.json", method = {RequestMethod.POST, RequestMethod.GET})
@ResponseBody
@ApiOperation(value = "根据id查询学生信息", notes = "查询学生", response = ModelAndView.class)
public ModelAndView selectStudentById(@ApiParam(required = true, value = "学生ID", name = "id") @RequestParam(value = "id") Integer id) {
return studentService.queryStudentById(id);
}
}
6,springSwaggerConfig加载到spring容器
在项目的配置文件applicantContext.xml中加入:
<!-- 将 springSwaggerConfig加载到spring容器 -->
<!-- 将自定义的swagger配置类加载到spring容器 -->
<bean class="com.zengqiang.utils.SwaggerConfig"/>
<mvc:resources mapping="/swagger/**" location="/swagger/"/>
好了,到这里就大工告成了。
运行我们的项目