、在pom.xml里添加jar包:

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger-ui</artifactId>
  4. <version>${springfox.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger2</artifactId>
  9. <version>${springfox.version}</version>
  10. </dependency>

在pom.xml里的properties里添加版本

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  4. <java.version>1.8</java.version>
  5. <springfox.version>2.5.0</springfox.version>
  6. </properties>

2、在com/demo下创建swagger文件夹,创建SwaggerConfig文件

  1. package com.demo.swagger;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.spi.DocumentationType;
  9. import springfox.documentation.spring.web.plugins.Docket;
  10. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  11. /**
  12. * Created by huguoju on 2016/12/29.
  13. */
  14. @Configuration
  15. @EnableSwagger2
  16. public class SwaggerConfig {
  17. /**
  18. * 可以定义多个组,比如本类中定义把test和demo区分开了 (访问页面就可以看到效果了)
  19. *
  20. */
  21. @Bean
  22. public Docket testApi() {
  23. return new Docket(DocumentationType.SWAGGER_2)
  24. .apiInfo(apiInfo())
  25. .select()
  26. .apis(RequestHandlerSelectors.basePackage("com.demo"))
  27. .paths(PathSelectors.any()).build();
  28. }
  29. private ApiInfo apiInfo() {
  30. return new ApiInfoBuilder()
  31. .title("Spring Boot中使用Swagger2构建RESTful APIs")
  32. .description("spring Boot 中构建RESTful API")
  33. .termsOfServiceUrl("")
  34. .contact("huguoju")
  35. .version("1.0")
  36. .build();
  37. }
  38. }

以上就完成了,在页面访问localhost:8080/swagger-ui.html,就看见了,下面主要说说怎么用

1、在com/dem/controller下创建TestController,

  1. package com.demo.controller;
  2. import com.demo.model.User;
  3. import com.demo.service.TestService;
  4. import io.swagger.annotations.Api;
  5. import io.swagger.annotations.ApiOperation;
  6. import io.swagger.annotations.ApiParam;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. /**
  14. * Created by huguoju on 2016/12/28.
  15. */
  16. @Controller
  17. @RequestMapping("test")
  18. @Api(value = "测试类",tags = "测试接口")
  19. public class TestController {
  20. @Autowired
  21. private TestService testService;
  22. @RequestMapping(value = "testData",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = {RequestMethod.POST,RequestMethod.GET})
  23. @ApiOperation("测试读写分离")
  24. public String testDateSource(
  25. @ApiParam(name = "userCode",value = "用户id",required = true)
  26. @RequestParam Integer userCode){
  27. User user=testService.selectByUserCode(userCode);
  28. Integer integer=testService.insertUser(user);
  29. return "oo";
  30. }
  31. }

以上就是controller里主要用到的@Api,  @ApiOperation ,@ApiParam

在model里使用@ApiModelProperty("")

  • import com.fasterxml.jackson.annotation.JsonIdentityInfo;
  • import io.swagger.annotations.ApiModel;
  • import io.swagger.annotations.ApiModelProperty;
  • import lombok.Data;
  • import org.springframework.stereotype.Component;
  • import java.util.Date;
  • @Data
  • public class User {
  • @ApiModelProperty("用户id")
  • private Integer userCode;
  • @ApiModelProperty("用户类型")
  • private String userType;
  • @ApiModelProperty("用户名称")
  • private String userName;
  • @ApiModelProperty("用户手机号")
  • private String mobileNumber;
  • }

springboot配置swagger2的更多相关文章

  1. JAVA入门[23]-SpringBoot配置Swagger2

    一.新建SpringBoot站点 1.新建module,然后引入pom依赖: <parent> <groupId>org.springframework.boot</gr ...

  2. SpringBoot配置swagger2(亲测有效,如果没有配置成功,欢迎在下方留言)

    一.导包: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...

  3. SSM非springboot配置swagger2

    前提:maven,ssm,不是springboot项目 1.在maven中添加依赖 <!-- Swagger2 Begin --> <!--springfox的核心jar包--> ...

  4. springboot 配置 swagger2

    1.pom.xml 添加依赖 <!--swagger2 依赖--> <dependency> <groupId>io.springfox</groupId&g ...

  5. IDEA springboot配置

    基于springboot2.1.7 springboot项目创建 springboot热部署 springboot配置swagger2 springboot配置mybatis springboot配置 ...

  6. springboot新增swagger2配置

    转自http://www.cnblogs.com/jtlgb/p/8532433.html SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人 ...

  7. SpringBoot集成Swagger2并配置多个包路径扫描

    1. 简介   随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...

  8. SpringBoot整合Swagger2,再也不用维护接口文档了!

    前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...

  9. SpringBoot(七):SpringBoot整合Swagger2

    原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...

随机推荐

  1. 微信小程序开发之微信支付

    微信支付是小程序开发中很重要的一个环节,下面会结合实战进行分析总结 环境准备 https服务器 微信小程序只支持https请求,因此需要配置https的单向认证服务(请参考 另一篇文章https受信证 ...

  2. selenium + ChromeDriver

    Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.而对于爬虫来说,使用Selenium操控浏览器来爬取网上的数据那么肯定是爬虫中的杀 ...

  3. 安徽省2016“京胜杯”程序设计大赛_H_单身晚会

    单身晚会 Time Limit: 1000 MS Memory Limit: 65536 KB Total Submissions: 53 Accepted: 16 Description ​ZJ和Z ...

  4. js复习---string

    对js的string的方法复习: 1.charCodeAt()  返回一个整数,代表指定位置字符串的unicode编码. strObj.charCodeAt(index) index 是处理字符的从零 ...

  5. python自动化运维二:业务服务监控

    p { margin-bottom: 0.25cm; line-height: 120% } a:link { } p { margin-bottom: 0.25cm; line-height: 12 ...

  6. [BZOJ 1054][HAOI 2008]移动玩具 状态压缩

    考试的时候一看是河南省选题,觉得会很难,有点不敢想正解.感觉是个状压.但是一看是十年前的题,那怂什么! 直接把十六个数的状态压进去,因为个数是不变的,所以状态枚举的时候只要找数目一样的转移即可.而且只 ...

  7. SNS社交系统“ThinkSNS V4.6”活动应用功能详解及应用场景举例

    sns社交系统ThinkSNS目前拥有功能:朋友圈(微博).微吧(论坛).频道.积分商城.IM即时聊天.直播.问答.活动.资讯(CMS).商城.广场.找人.搜索.评论.点赞.转发.分享.话题.积分.充 ...

  8. Hadoop分布式集群搭建hadoop2.6+Ubuntu16.04

    前段时间搭建Hadoop分布式集群,踩了不少坑,网上很多资料都写得不够详细,对于新手来说搭建起来会遇到很多问题.以下是自己根据搭建Hadoop分布式集群的经验希望给新手一些帮助.当然,建议先把HDFS ...

  9. 01-artDialog4.1.7常用整理

    关闭弹出框的几种常见方法: 1,artDialog可以通过鼠标双击关闭对话框,esc关闭对话框等.为了在关闭对话框要执行某一方法,不能仅仅在弹框中的关闭按钮中写.这时提供了一个函数:close.无论在 ...

  10. MATLAB中绘制图形的时候,坐标和标题倒置

    1.如上图所示,直方图的坐标轴以及标题文字都颠倒了 原因: 在MATLAB显示的subplot函数中,图像与直方图这些不属于一类,所以在显示的时候会出现这种情况 解决办法:1>将图像与直方图分开 ...