我是在ssm框架下集成swagger的,具体的ssm搭建可以看这篇博文:

Intellij Idea下搭建基于Spring+SpringMvc+MyBatis的WebApi接口架构

本项目的GitHub地址:https://github.com/chenyangsocool/ssm.git

接下去就正式开始了:

1.通过maven导入相关swagger的jar包:

  1. <!-- swagger -->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.6.</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>io.springfox</groupId>
  9. <artifactId>springfox-swagger-ui</artifactId>
  10. <version>2.6.</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.fasterxml.jackson.core</groupId>
  14. <artifactId>jackson-core</artifactId>
  15. <version>2.6.</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.fasterxml.jackson.core</groupId>
  19. <artifactId>jackson-databind</artifactId>
  20. <version>2.6.</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>com.fasterxml.jackson.core</groupId>
  24. <artifactId>jackson-annotations</artifactId>
  25. <version>2.6.</version>
  26. </dependency>
  27. <!-- /swagger -->

2.在com.chenyangsocool.ssm.tools下新建swagger/Swagger2Config.java:

  1. package com.chenyangsocool.ssm.tools.swagger;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  5. import io.swagger.annotations.ApiOperation;
  6. import springfox.documentation.builders.ApiInfoBuilder;
  7. import springfox.documentation.builders.PathSelectors;
  8. import springfox.documentation.builders.RequestHandlerSelectors;
  9. import springfox.documentation.service.ApiInfo;
  10. import springfox.documentation.spi.DocumentationType;
  11. import springfox.documentation.spring.web.plugins.Docket;
  12. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  13.  
  14. @Configuration //让Spring来加载该类配置
  15. @EnableWebMvc //启用Mvc,非springboot框架需要引入注解@EnableWebMvc
  16. @EnableSwagger2 //启用Swagger2
  17. public class Swagger2Config {
  18. @Bean
  19. public Docket createRestApi() {
  20. return new Docket(DocumentationType.SWAGGER_2)
  21. .apiInfo(apiInfo()).select()
  22. //扫描指定包中的swagger注解
  23. //.apis(RequestHandlerSelectors.basePackage("com.xia.controller"))
  24. //扫描所有有注解的api,用这种方式更灵活
  25. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
  26. .paths(PathSelectors.any())
  27. .build();
  28. }
  29. private ApiInfo apiInfo() {
  30. return new ApiInfoBuilder()
  31. .title("SSM RESTful APIs")
  32. .description("This API Document is based on RESTful Style, The description is detail and auto-generation, It's very friendly for developers.")
  33. .termsOfServiceUrl("http://www.cnblogs.com/chenyangsocool/")
  34. .contact("Young")
  35. .version("1.0.0")
  36. .build();
  37. }
  38. }

3.在实体类中创建注释,以Test实体类为例:

  1. package com.chenyangsocool.ssm.model;
  2.  
  3. import io.swagger.annotations.ApiModel;
  4. import io.swagger.annotations.ApiModelProperty;
  5.  
  6. @ApiModel(value="Test",description="Test")//对类进行swagger注解
  7. public class Test {
  8. @ApiModelProperty(value="测试id",name="id")//对类的字段属性进行swagger注解
  9. private int id;
  10.  
  11. @ApiModelProperty(value="测试内容",name="context")
  12. private String context;
  13.  
  14. @ApiModelProperty(value="测试内容的浏览数",name="viewCount")
  15. private int viewCount;
  16.  
  17. public int getId() {
  18. return id;
  19. }
  20.  
  21. public void setId(int id) {
  22. this.id = id;
  23. }
  24.  
  25. public String getContext() {
  26. return context;
  27. }
  28.  
  29. public void setContext(String context) {
  30. this.context = context;
  31. }
  32.  
  33. public int getViewCount() {
  34. return viewCount;
  35. }
  36.  
  37. public void setViewCount(int viewCount) {
  38. this.viewCount = viewCount;
  39. }
  40.  
  41. @Override
  42. public String toString() {
  43. return "Test{" +
  44. "id=" + id +
  45. ", context='" + context + '\'' +
  46. ", viewCount=" + viewCount +
  47. '}';
  48. }
  49. }

4.在TestController中添加swagger相关代码:

  1. package com.chenyangsocool.ssm.controller;
  2.  
  3. import com.chenyangsocool.ssm.model.Test;
  4. import com.chenyangsocool.ssm.service.ITestService;
  5. import io.swagger.annotations.Api;
  6. import io.swagger.annotations.ApiOperation;
  7. import org.springframework.stereotype.Controller;
  8.  
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import org.springframework.ui.Model;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12.  
  13. import javax.annotation.Resource;
  14. import javax.servlet.http.HttpServletRequest;
  15.  
  16. @Controller
  17. @RequestMapping("/test")
  18. @Api(value = "测试信息", tags = {"测试相关接口"})//swagger控制器说明注解
  19. public class TestController {
  20.  
  21. @Resource
  22. private ITestService testService;
  23.  
  24. @RequestMapping("/index_page")
  25. public String showIndex(HttpServletRequest request, Model model) {
  26. int id = Integer.parseInt(request.getParameter("id"));
  27. Test test = this.testService.getModelById(id);
  28. //绑定对象到test/index.jsp
  29. model.addAttribute("test", test);
  30. return "test/index";
  31. }
  32.  
  33. @RequestMapping("/index_api")
  34. @ResponseBody
  35. @ApiOperation(value = "获取单个测试实例", notes = "传入一个id,获取该id对应的实例。",httpMethod = "GET")//swagger方法注解
  36. public Test Index(HttpServletRequest request,Model model) {
  37. int id = Integer.parseInt(request.getParameter("id"));
  38. return this.testService.getModelById(id);
  39. }
  40. }

5.访问:

http://localhost:8080/ssm/swagger-ui.html

即可查看所定义的api接口列表:

注:

更多注解可以网上自己搜索

本项目的GitHub地址:https://github.com/chenyangsocool/ssm.git

后续思考:

实体类是由mybatis自动生成,那实体类中的swagger注解是否也可以由mybatis自动生成?

等我有空就把文章写上来~~

参考文章:

SpringMVC集成Swagger插件以及Swagger注解的简单使用

在spring+springMvc+mabatis框架下集成swagger的更多相关文章

  1. Spring+SpringMvc+Mybatis框架集成搭建教程

    一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...

  2. SSM(Spring + Springmvc + Mybatis)框架面试题

    JAVA SSM框架基础面试题https://blog.csdn.net/qq_39031310/article/details/83050192 SSM(Spring + Springmvc + M ...

  3. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建四:配置springmvc

    在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试的基础上 继续进行springmvc的配置 一:配置完善web.xml文件

  4. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)

    引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一   的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...

  5. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一:建立MAVEN Web项目

    一:创建maven web项目er

  6. SSH(Spring SpringMVC Hibernate)框架整合

    项目说明: 使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能 项目结构   1.导入依赖jar包 <!--单测--> <dependency&g ...

  7. SSM(Spring +SpringMVC + Mybatis)框架搭建

    SSM(Spring +SpringMVC + Mybatis)框架的搭建 最近通过学习别人博客发表的SSM搭建Demo,尝试去搭建一个简单的SSMDemo---实现的功能是对用户增删改查的操作 参考 ...

  8. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee.com/niceyoo/jeenotes-ssm 2. 概述 在写代码之前我们先了解一下 ...

  9. SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释(转)

    原文:https://blog.csdn.net/yijiemamin/article/details/51156189# 这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文 ...

随机推荐

  1. flex弹性盒子

    注意事项 1.设为Flex布局之后,子元素的float,clear和vertical-align属性都讲失效 2.采用Flex布局的元素,称为Flex容器(Flex container),所有的子元素 ...

  2. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  3. php中的var_dump()方法的详细说明

    首先看看实例: <?PHP$a = "alsdflasdf;a";$b = var_dump($a);echo "<br>";//var_du ...

  4. RHEL内核源码编译

    http://blog.csdn.net/lishenglong666/article/details/7320864 http://ftp.redhat.com/pub/redhat/linux/e ...

  5. 原生JavaScript---正则表达式

    JavaScript 中正则的性能比想象中的低很多.能用字符串方法搞定的,尽量别用正则.------玉伯 抛开性能不谈,一起来看看正则表达式怎么用吧! 先看看JavaScript正则表达式中一些特殊字 ...

  6. pytest文档10-命令行传参

    前言 命令行参数是根据命令行选项将不同的值传递给测试函数,比如平常在cmd执行"pytest --html=report.html",这里面的"--html=report ...

  7. booksleeve 使用

    By offering pipelined, asynchronous, multiplexed and thread-safe access to redis, BookSleeve enables ...

  8. 从两个TIMESTAMP中获取时间差(秒)

    When you subtract two variables of type TIMESTAMP, you get an INTERVAL DAY TO SECOND which includes ...

  9. JavaScript的学习要点

    概要 了解Javascript历史以及Javascript三个不同组成部分: ECMAScript DOM(文档对象模型) BOM(浏览器对象模型) ECMAScript 目标 掌握Javascrip ...

  10. 如何移植openwrt系统

    Cisco/Linksys在2003年发布了WRT54G这款无线路由器,同年有人发现它的IOS是基于Linux的,然而Linux是基于GPL许可证发布的,按照该许可证Cisco应该把WRT54G的IO ...