springboot+swagger2案例
1.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>swagger</groupId>
<artifactId>swagger</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency> </dependencies>
</project>
swagger2配置文件
package com.newtouch.swagger.config; 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; @Configuration
@EnableSwagger2
public class SwaggerConfiguration { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.newtouch.swagger.controller"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot中使用Swagger2构建RESTfulAPIs")
.description("SpringBoot中使用Swagger2构建RESTfulAPIs")
.termsOfServiceUrl("http://blog.didispace.com/")
.contact("程序猿DD")
.version("1.0")
.build();
} }
请求bean
package com.newtouch.swagger.controller.bean; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; @ApiModel("用户")
public class UserRequest { @ApiModelProperty(value="用户id",name="id",required=false)
private int id; @ApiModelProperty(value="用户name",name="name",required=false)
private String name; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
controller
package com.newtouch.swagger.controller; import java.util.HashMap;
import java.util.Map; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.newtouch.swagger.controller.bean.UserRequest; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; @Api("用户操作类")
@RestController
public class UserController { @RequestMapping(value="/getName")
@ApiOperation("用户对象")
public Map getName(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) UserRequest u) {
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("id", u.getId());
resultMap.put("name", u.getName()); return resultMap;
} }
效果展示

swagger注解详解:https://blog.csdn.net/u014231523/article/details/76522486
springboot+swagger2案例的更多相关文章
- SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法
原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...
- springboot+swagger2
springboot+swagger2 小序 新公司的第二个项目,是一个配置管理终端机(比如:自动售卖机,银行取款机)的web项目,之前写过一个分模块的springboot框架,就在那个框架基础上进行 ...
- SpringBoot开发案例之多任务并行+线程池处理
前言 前几篇文章着重介绍了后端服务数据库和多线程并行处理优化,并示例了改造前后的伪代码逻辑.当然了,优化是无止境的,前人栽树后人乘凉.作为我们开发者来说,既然站在了巨人的肩膀上,就要写出更加优化的程序 ...
- SpringBoot开发案例从0到1构建分布式秒杀系统
前言 最近,被推送了不少秒杀架构的文章,忙里偷闲自己也总结了一下互联网平台秒杀架构设计,当然也借鉴了不少同学的思路.俗话说,脱离案例讲架构都是耍流氓,最终使用SpringBoot模拟实现了部分秒杀场 ...
- SpringBoot+Swagger2 整合
SpringBoot+Swagger2四步整合 第一步:添加相关依赖 <parent> <groupId>org.springframework.boot</groupI ...
- Spring-boot官方案例分析之data-jpa
Spring-boot官方案例分析之data-jpa package sample.data.jpa; import org.junit.Before; import org.junit.Test; ...
- Spring-boot官方案例分析之log4j
Spring-boot官方案例分析之log4j 运行单元测试分析: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfigur ...
- Xmemcached与SpringBoot实际案例
在本人的这篇文章<Xmemcached集群与SpringBoot整合>基础上,进行XMemcached与SpringBoot实际案例的结合. 有以下这张表,将这张表的增删改查操作都添加到X ...
- Springboot+swagger2.7集成开发
Springboot+swagger2.7集成开发 本篇文章是介绍最新的springboot和swagger2.7集成开发和2.0稍微有一些出入: Springboot集成环境配置 Swagger2. ...
随机推荐
- CPP_封装_继承_多态
类的三方法:封装,继承,多态.封装:使用一整套方法去创建一个新的类型,这叫类的封装.继承:从一个现有的类型基础上,稍作改动,得到一个新的类型的方法,叫类的继承.多态:当有几个不同的子类对象时,对象调用 ...
- ElasticSearch 深度分页解决方案 {"index":{"number_of_replicas":0}}
常见深度分页方式 from+size es 默认采用的分页方式是 from+ size 的形式,在深度分页的情况下,这种使用方式效率是非常低的,比如 from = 5000, size=10, es ...
- WaitForSingleObject函数的使用
等待函数可使线程自愿进入等待状态,直到一个特定的内核对象变为已通知状态为止. WaitForSingleObject 函数 DWORD WaitForSingleObject( HANDLE hOb ...
- Java知多少(17)强调一下编程风格
讲完了Java的基础语法,大家就可以编写简单的程序代码了,这里有必要强调一下编程风格. 代码风格虽然不影响程序的运行,但对程序的可读性却非常重要.自己编写的程序要让别人看懂,首先在排版方面要非常注意. ...
- 树莓派3 U盘启动 配置 & 即 MSD启动 总结
树莓派3添加了一个新特性:允许USB启动.现在我们既可以从SD卡启动,也可以从USB启动.USB设备可以是U盘,带USB适配器的SSD硬盘,甚至是移动硬盘. 本文介绍怎么从U盘启动树莓派3. 1. ...
- 使用x11vnc作为vncserver端
1 安装x11vnc $ sudo apt-get update $ sudo apt-get install x11vnc 2 生成密码 $ x11vnc -storepasswd Enter VN ...
- Opengl绘制我们的小屋(四)第三人称漫游
本节内容是在第一人称漫游上完成的,请先了解上文中第一人称漫游的实现. 这一节讲下第三人称漫游是如何实现,第三人称,简单来说,就是在你后面会跟着一台摄像机顺着你拍摄. 先看一下失败的尝试.这个方法是把人 ...
- SAP DBDI 网银接口实现案例
在财务共享中心SSC实施中,为了提高AP和对账的效率,不可避免的需要实现和网上银行的集成.笔者为各位分析该案例如下: 为什么要上网银? 2).和SAP的无缝集成 3).直接在SAP中和银联对接 4). ...
- unity--------shader之standard 标准参数
[Unity3D自学记录]Unity5 之 standard参数 标签: unity3d 2016-07-13 10:17 2428人阅读 评论(0) 收藏 举报 分类: Unity3D(70) ...
- 使用appledoc 生成技术API文档具体解释
一. 首先安装 appledoc 第一步:使用终端命令进行下载安装 git clone git://github.com/tomaz/appledoc.git cd ./appledoc sudo s ...