SpringBoot2.0 整合 Swagger2 ,构建接口管理界面
一、Swagger2简介
1、Swagger2优点
整合到Spring Boot中,构建强大RESTful API文档。省去接口文档管理工作,修改代码,自动更新,Swagger2也提供了强大的页面测试功能来调试RESTful API。
2、Swagger2常用注解
Api:修饰整个类,描述Controller的作用
ApiOperation:描述一个类的一个方法,或者说一个接口
ApiParam:单个参数描述
ApiModel:用对象来接收参数
ApiProperty:用对象接收参数时,描述对象的一个字段
ApiResponse:HTTP响应其中1个描述
ApiResponses:HTTP响应整体描述
ApiIgnore:使用该注解忽略这个API
ApiError :发生错误返回的信息
ApiImplicitParam:一个请求参数
ApiImplicitParams:多个请求参数
二、与SpringBoot2.0 整合
1、核心依赖
spring-boot:2.1.3.RELEASE
swagger:2.6.1
2、Swagger2 配置
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;
/**
* Swagger 配置文件
*/
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.two"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot利用Swagger构建API文档")
.description("使用RestFul风格, 创建人:知了一笑")
.termsOfServiceUrl("https://github.com/cicadasmile")
.version("version 1.0")
.build();
}
}
3、启动类添加注解
@EnableSwagger2
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class,args) ;
}
}
4、启动效果图
三、增删改查案例
1、添加用户
(1)、代码块
@ApiOperation(value="添加用户", notes="创建新用户")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public ResponseEntity<JsonResult> addUser (@RequestBody User user){
JsonResult result = new JsonResult();
try {
users.put(user.getId(), user);
result.setResult(user.getId());
result.setStatus("ok");
} catch (Exception e) {
result.setResult("服务异常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果图
2、用户列表
(1)、代码块
@ApiOperation(value="用户列表", notes="查询用户列表")
@RequestMapping(value = "/getUserList", method = RequestMethod.GET)
public ResponseEntity<JsonResult> getUserList (){
JsonResult result = new JsonResult();
try {
List<User> userList = new ArrayList<>(users.values());
result.setResult(userList);
result.setStatus("200");
} catch (Exception e) {
result.setResult("服务异常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果图
3、用户查询
(1)、代码块
@ApiOperation(value="用户查询", notes="根据ID查询用户")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
@RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Integer id){
JsonResult result = new JsonResult();
try {
User user = users.get(id);
result.setResult(user);
result.setStatus("200");
} catch (Exception e) {
result.setResult("服务异常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果图
4、更新用户
(1)、代码块
@ApiOperation(value="更新用户", notes="根据Id更新用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long",paramType = "path"),
@ApiImplicitParam(name = "user", value = "用户对象user", required = true, dataType = "User")
})
@RequestMapping(value = "/updateById/{id}", method = RequestMethod.PUT)
public ResponseEntity<JsonResult> updateById (@PathVariable("id") Integer id, @RequestBody User user){
JsonResult result = new JsonResult();
try {
User user1 = users.get(id);
user1.setUsername(user.getUsername());
user1.setAge(user.getAge());
users.put(id, user1);
result.setResult(user1);
result.setStatus("ok");
} catch (Exception e) {
result.setResult("服务异常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果图
5、删除用户
(1)、代码块
@ApiOperation(value="删除用户", notes="根据id删除指定用户")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
@RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
public ResponseEntity<JsonResult> deleteById (@PathVariable(value = "id") Integer id){
JsonResult result = new JsonResult();
try {
users.remove(id);
result.setResult(id);
result.setStatus("ok");
} catch (Exception e) {
result.setResult("服务异常");
result.setStatus("500");
e.printStackTrace();
}
return ResponseEntity.ok(result);
}
(2)、效果图
四、源代码
GitHub地址:知了一笑
https://github.com/cicadasmile/middle-ware-parent
码云地址:知了一笑
https://gitee.com/cicadasmile/middle-ware-parent

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
一篇文章搞明白Integer、new Integer() 和 int 的概念与区别
基本概念的区分 1、Integer 是 int 的包装类,int 则是 java 的一种基本数据类型2、Integer 变量必须实例化后才能使用,而int变量不需要3、Integer 实际是对象的引用,当new一个 Integer时,实际上是生成一个指针指向此对象;而 int 则是直接存储数据值4、Integer的默认值是null,int的默认值是0 Integer、new Integer() 和 int 的比较 1、两个 new Integer() 变量比较 ,永远是 false因为new生成的是两个对象,其内存地址不同 Integer i = new Integer(100); Integer j = new Integer(100); System.out.print(i == j); //false 2、Integer变量 和 new Integer() 变量比较 ,永远为 false。因为 Integer变量 指向的是 java 常量池 中的对象,而 new Integer() 的变量指向 堆中 新建的对象,两者在内存中的地址不同。 Integer i = new Integ...
-
下一篇
Java描述设计模式(02):简单工厂模式
一、生活场景简介 1、引入场景 订餐流程简单描述 1)、食品抽象类,规定食品的基础属性操作 2)、鱼类,鸡肉类食品类扩展 3)、订餐流程类,根据食品名称,加工指定类型食品 4)、模拟客户端预定操作 2、源代码实现 1)、关系图谱 2)、代码实现 /** * 简单工厂模式引入场景 */ public class C01_InScene { public static void main(String[] args) { OrderFood1 orderFood = new OrderFood1() ; orderFood.orderFood("fish") ; orderFood.orderFood("chicken") ; } } /** * 订餐流程 */ class OrderFood1 { public Food1 orderFood (String foodName){ Food1 food1 = null ; if (foodName.equals("fish")){ food1 = new FishFood1() ; food1.setName("黑鱼"); } else ...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8编译安装MySQL8.0.19
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS7,8上快速安装Gitea,搭建Git服务器
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- CentOS关闭SELinux安全模块
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果