在spring+springMvc+mabatis框架下集成swagger
我是在ssm框架下集成swagger的,具体的ssm搭建可以看这篇博文:
Intellij Idea下搭建基于Spring+SpringMvc+MyBatis的WebApi接口架构
本项目的GitHub地址:https://github.com/chenyangsocool/ssm.git
接下去就正式开始了:
1.通过maven导入相关swagger的jar包:
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.6.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.6.5</version> </dependency> <!-- /swagger -->
2.在com.chenyangsocool.ssm.tools下新建swagger/Swagger2Config.java:
package com.chenyangsocool.ssm.tools.swagger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import io.swagger.annotations.ApiOperation; 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; @Configuration //让Spring来加载该类配置 @EnableWebMvc //启用Mvc,非springboot框架需要引入注解@EnableWebMvc @EnableSwagger2 //启用Swagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()).select() //扫描指定包中的swagger注解 //.apis(RequestHandlerSelectors.basePackage("com.xia.controller")) //扫描所有有注解的api,用这种方式更灵活 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SSM RESTful APIs") .description("This API Document is based on RESTful Style, The description is detail and auto-generation, It's very friendly for developers.") .termsOfServiceUrl("http://www.cnblogs.com/chenyangsocool/") .contact("Young") .version("1.0.0") .build(); } }
3.在实体类中创建注释,以Test实体类为例:
package com.chenyangsocool.ssm.model; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel(value="Test",description="Test")//对类进行swagger注解 public class Test { @ApiModelProperty(value="测试id",name="id")//对类的字段属性进行swagger注解 private int id; @ApiModelProperty(value="测试内容",name="context") private String context; @ApiModelProperty(value="测试内容的浏览数",name="viewCount") private int viewCount; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getContext() { return context; } public void setContext(String context) { this.context = context; } public int getViewCount() { return viewCount; } public void setViewCount(int viewCount) { this.viewCount = viewCount; } @Override public String toString() { return "Test{" + "id=" + id + ", context='" + context + '\'' + ", viewCount=" + viewCount + '}'; } }
4.在TestController中添加swagger相关代码:
package com.chenyangsocool.ssm.controller; import com.chenyangsocool.ssm.model.Test; import com.chenyangsocool.ssm.service.ITestService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @Controller @RequestMapping("/test") @Api(value = "测试信息", tags = {"测试相关接口"})//swagger控制器说明注解 public class TestController { @Resource private ITestService testService; @RequestMapping("/index_page") public String showIndex(HttpServletRequest request, Model model) { int id = Integer.parseInt(request.getParameter("id")); Test test = this.testService.getModelById(id); //绑定对象到test/index.jsp model.addAttribute("test", test); return "test/index"; } @RequestMapping("/index_api") @ResponseBody @ApiOperation(value = "获取单个测试实例", notes = "传入一个id,获取该id对应的实例。",httpMethod = "GET")//swagger方法注解 public Test Index(HttpServletRequest request,Model model) { int id = Integer.parseInt(request.getParameter("id")); return this.testService.getModelById(id); } }
5.访问:
http://localhost:8080/ssm/swagger-ui.html
即可查看所定义的api接口列表:
注:
更多注解可以网上自己搜索
本项目的GitHub地址:https://github.com/chenyangsocool/ssm.git
后续思考:
实体类是由mybatis自动生成,那实体类中的swagger注解是否也可以由mybatis自动生成?
等我有空就把文章写上来~~
参考文章:
SpringMVC集成Swagger插件以及Swagger注解的简单使用
** Then I looked up at the sky and saw the sun **
posted on 2018-03-14 09:15 chenyangsocool 阅读(486) 评论(0) 编辑 收藏 举报