springboot配置swagger2
、在pom.xml里添加jar包:
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>${springfox.version}</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>${springfox.version}</version>
- </dependency>
在pom.xml里的properties里添加版本
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- <springfox.version>2.5.0</springfox.version>
- </properties>
2、在com/demo下创建swagger文件夹,创建SwaggerConfig文件
- package com.demo.swagger;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- 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;
- /**
- * Created by huguoju on 2016/12/29.
- */
- @Configuration
- @EnableSwagger2
- public class SwaggerConfig {
- /**
- * 可以定义多个组,比如本类中定义把test和demo区分开了 (访问页面就可以看到效果了)
- *
- */
- @Bean
- public Docket testApi() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.demo"))
- .paths(PathSelectors.any()).build();
- }
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("Spring Boot中使用Swagger2构建RESTful APIs")
- .description("spring Boot 中构建RESTful API")
- .termsOfServiceUrl("")
- .contact("huguoju")
- .version("1.0")
- .build();
- }
- }
以上就完成了,在页面访问localhost:8080/swagger-ui.html,就看见了,下面主要说说怎么用
1、在com/dem/controller下创建TestController,
- package com.demo.controller;
- import com.demo.model.User;
- import com.demo.service.TestService;
- 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.http.MediaType;
- 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;
- /**
- * Created by huguoju on 2016/12/28.
- */
- @Controller
- @RequestMapping("test")
- @Api(value = "测试类",tags = "测试接口")
- public class TestController {
- @Autowired
- private TestService testService;
- @RequestMapping(value = "testData",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = {RequestMethod.POST,RequestMethod.GET})
- @ApiOperation("测试读写分离")
- public String testDateSource(
- @ApiParam(name = "userCode",value = "用户id",required = true)
- @RequestParam Integer userCode){
- User user=testService.selectByUserCode(userCode);
- Integer integer=testService.insertUser(user);
- return "oo";
- }
- }
以上就是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的更多相关文章
- JAVA入门[23]-SpringBoot配置Swagger2
一.新建SpringBoot站点 1.新建module,然后引入pom依赖: <parent> <groupId>org.springframework.boot</gr ...
- SpringBoot配置swagger2(亲测有效,如果没有配置成功,欢迎在下方留言)
一.导包: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...
- SSM非springboot配置swagger2
前提:maven,ssm,不是springboot项目 1.在maven中添加依赖 <!-- Swagger2 Begin --> <!--springfox的核心jar包--> ...
- springboot 配置 swagger2
1.pom.xml 添加依赖 <!--swagger2 依赖--> <dependency> <groupId>io.springfox</groupId&g ...
- IDEA springboot配置
基于springboot2.1.7 springboot项目创建 springboot热部署 springboot配置swagger2 springboot配置mybatis springboot配置 ...
- springboot新增swagger2配置
转自http://www.cnblogs.com/jtlgb/p/8532433.html SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人 ...
- SpringBoot集成Swagger2并配置多个包路径扫描
1. 简介 随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...
- SpringBoot整合Swagger2,再也不用维护接口文档了!
前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...
- SpringBoot(七):SpringBoot整合Swagger2
原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...
随机推荐
- excel下拉级联的做法
前面的文章讲了,excel下拉级联,重新选第一个下拉,后面那个值怎么清除.今天我讲下excel利用宏解决整个表格的级联下拉问题. 我遇到的情况是两个下垃圾连,第一个医生类别,第二个医生职称,而且我是要 ...
- AsyncTask用法解析-下载文件动态更新进度条
1. 泛型 AysncTask<Params, Progress, Result> Params:启动任务时传入的参数,通过调用asyncTask.execute(param)方法传入. ...
- 蓝桥杯算法训练_2的次幂表示+前缀表达式+Anagrams问题+出现次数最多的整数
今天做了4个简单的题,题目虽然是简单,但是对于我这样的小白,还是有很多东西需要学习的. 2的次幂表示 上面就是题目,题目说的也很清晰了,接下来就是递归的实现: #include<iostream ...
- MyEclipse去除网上复制下来的代码带有的行号
正则表达式去除代码行号 作为开发人员,我们经常从网上复制一些代码,有些时候复制的代码前面是带有行号,如: MyEclipse本身自带有查找替换功能,并且支持正则表达式替换,使用正则替换就可以很容易去除 ...
- 使用 Node.js 搭建一个 API 网关
原文地址:Building an API Gateway using Node.js 外部客户端访问微服务架构中的服务时,服务端会对认证和传输有一些常见的要求.API 网关提供共享层来处理服务协议之间 ...
- 【有意思的BUG】客户端无厘头 已连网的场景初始化太慢 未连网的场景异常崩溃
客户端 已连网的场景初始化太慢 当在未连接internet的时候,打开某些APP,会比较迅速地初始化进入到主页面. 但是当我在已经连接了internet的时候,打开某些APP,有些会初始化很久!!!! ...
- python时间序列分析
题记:毕业一年多天天coding,好久没写paper了.在这动荡的日子里,也希望写点东西让自己静一静.恰好前段时间用python做了一点时间序列方面的东西,有一丁点心得体会想和大家 ...
- 【每天一道算法题】时间复杂度为O(n)的排序
有1,2,……一直到n的无序数组,求排序算法,并且要求时间复杂度为O(n),空间复杂度为O(1),使用交换,而且一次只能交换两个数. 这个是以前看到的算法题,题目不难.但是要求比较多,排序算法中,时间 ...
- Jenkins : 邮件通知
目录 全局配置 为项目添加邮件通知 邮件模板 Pipeline 支持 总结 Jenkins 内置了 Mailer 插件用于发送邮件通知,但是 Mailer 插件的功能比较简单,无法按照用户的需求进行邮 ...
- mysql-索引与优化
写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将 ...