Spring Boot 系列教程9-swagger-前后端分离后的标准
前后端分离的必要
- 现在的趋势发展,需要把前后端开发和部署做到真正的分离
- 做前端的谁也不想用Maven或者Gradle作为构建工具
- 做后端的谁也不想要用Grunt或者Gulp作为构建工具
前后端需要通过接口来协作
- 可能是JSON格式的RESTFul的接口
- 可能是XML的接口
- 重点是后台只负责数据的提供和处理,而完全不处理展现
- 而前端则负责拿到数据,组织数据并开始展现的工作
Swagger
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
Swagger API显示效果
项目图片
pom.xml
<!-- Swagger2强大RESTful API文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
Swagger2
package com.jege.spring.boot.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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
// http://localhost:8080/swagger-ui.html
// Swagger2默认将所有的Controller中的RequestMapping方法都会暴露,
// 然而在实际开发中,我们并不一定需要把所有API都提现在文档中查看,这种情况下,使用注解
// @ApiIgnore来解决,如果应用在Controller范围上,则当前Controller中的所有方法都会被忽略,
// 如果应用在方法上,则对应用的方法忽略暴露API
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.jege.spring.boot.controller")).paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("je-ge的浆糊").description("je-ge的浆糊")
.termsOfServiceUrl("http://blog.csdn.net/je_ge").contact("je-ge").version("1.0").build();
}
}
UserController
package com.jege.spring.boot.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.jege.spring.boot.data.jpa.entity.User;
import com.jege.spring.boot.data.jpa.repository.UserRepository;
import com.jege.spring.boot.json.AjaxResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:用户CRUD操作
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserRepository userRepository;
// 显示用户列表
@RequestMapping("/list")
public String list() {
return "user";
}
// 显示用户json数据
@ApiOperation(value = "获取用户列表,支持分页", notes = "json方法获取用户列表")
@ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataType = "int"),
@ApiImplicitParam(name = "rows", value = "每页条数", required = true, dataType = "int") })
@RequestMapping("/json")
@ResponseBody
public Map<String, Object> json(@RequestParam(name = "page", defaultValue = "1") int page,
@RequestParam(name = "rows", defaultValue = "10") int rows) {
Pageable pageable = new PageRequest(page - 1, rows);
return findEasyUidata(userRepository.findAll(pageable));
}
private <T> Map<String, Object> findEasyUidata(Page<T> page) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("rows", page.getContent());
map.put("total", page.getTotalElements());
return map;
}
// 处理保存
@ApiOperation(value = "保存用户", notes = "根据User对象操作用户")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
@RequestMapping("/save")
@ResponseBody
public AjaxResult save(User user) {
userRepository.save(user);
return new AjaxResult().success();
}
// 处理删除
@ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
@RequestMapping("/delete")
@ResponseBody
public AjaxResult delete(Long id) {
userRepository.delete(id);
return new AjaxResult().success();
}
}
访问地址swagger的地址
http://localhost:8080/swagger-ui.html
其他关联代码
- Spring Boot 系列教程7-EasyUI-datagrid
http://blog.csdn.net/je_ge/article/details/53365189 - Spring Boot 系列教程5-热部署-devtools模块
http://blog.csdn.net/je_ge/article/details/53326525 - Spring Boot 系列教程2-Data JPA
http://blog.csdn.net/je_ge/article/details/53294949
源码地址
https://github.com/je-ge/spring-boot
如果觉得我的文章对您有帮助,请予以打赏。您的支持将鼓励我继续创作!谢谢!
Spring Boot 系列教程9-swagger-前后端分离后的标准的更多相关文章
- 手把手教你使用 Spring Boot 3 开发上线一个前后端分离的生产级系统(一) - 介绍
项目简介 novel 是一套基于时下最新 Java 技术栈 Spring Boot 3 + Vue 3 开发的前后端分离的学习型小说项目,配备详细的项目教程手把手教你从零开始开发上线一个生产级别的 J ...
- Spring Boot Security JWT 整合实现前后端分离认证示例
前面两章节我们介绍了 Spring Boot Security 快速入门 和 Spring Boot JWT 快速入门,本章节使用 JWT 和 Spring Boot Security 构件一个前后端 ...
- Swagger - 前后端分离后的契约
前后端分离 按照现在的趋势,前后端分离几乎已经是业界对开发和部署方式所达成的一种共识.所谓的前后端分离,并不是传统行业中的按部门划分,一部分人只做前端(HTML/CSS/JavaScript等等),另 ...
- 全栈的自我修养: 001环境搭建 (使用Vue,Spring Boot,Flask,Django 完成Vue前后端分离开发)
全栈的自我修养: 环境搭建 Not all those who wander are lost. 彷徨者并非都迷失方向. Table of Contents @ 目录 前言 环境准备 nodejs v ...
- Spring Boot 系列教程15-页面国际化
internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...
- Spring Boot 系列教程11-html页面解析-jsoup
需求 需要对一个页面进行数据抓取,并导出doc文档 html解析器 jsoup 可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操 ...
- Spring Boot 系列教程7-EasyUI-datagrid
jQueryEasyUI jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要 ...
- Spring Boot 系列教程19-后台验证-Hibernate Validation
后台验证 开发项目过程中,后台在很多地方需要进行校验操作,比如:前台表单提交,调用系统接口,数据传输等.而现在多数项目都采用MVC分层式设计,每层都需要进行相应地校验. 针对这个问题, JCP 出台一 ...
- Spring Boot 系列教程18-itext导出pdf下载
Java操作pdf框架 iText是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有很好 ...
随机推荐
- stl实现结构体排序关键语法要点(sort)
sort函数,调用时使用函数头: #include <algorithm> sort(begin,end);用来表示一个范围. int _tmain(int argc, _TCHAR* a ...
- Redis持久存储-AOF&RDB
Redis中数据存储模式有2种:cache-only,persistence;cache-only即只做为"缓存"服务,不持久数据,数据在服务终止后将消失,此模式下也将不存在&qu ...
- C语言 - 预编译
1.#ifdef 实现 与 或#if (defined SIMULATION) && (defined _FMM_LOG)#endif #if (!defined A) || (!de ...
- 飞雪桌面日历软件 V8.6 免费绿色版
软件名称: 飞雪桌面日历软件软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP / Win2008软件大小: 4MB图片预览: 软件简介: ...
- 笔记一:OOAD与UML
一.面向对象的概念与方法 1. 面向对象 1.1. 面向对象是一种系统建模技术 1.2. 面向对象编程是按照OO的方法学来开发程序的过程 1.3. 通过分析系统内对象的交互来描述或建模一个系统 1. ...
- 总结一下C++各个版本之间的功能扩充
活到老,学到老. C++ 98 我们学习和教材中常见的. C++ 03 主要是对98版本进行了bug修复. C++ 11 引入的新功能请参见: http://www.cpluspl ...
- MySQL5.7以上开启binlog
在my.cnf的mysqld下加入: server_id = 0 log_bin=/harddisk/mysql_data/mysql_binlog/mysql-bin binlog_format ...
- html5 --基础笔记2
1.autocomplete 可以给表单本身(不是fieldset)设置属性来禁用整个表单的自动完成功能 <form id="" method="" au ...
- Windows下为Python编译C扩展模块
工具:CodeBlocks 13.12 步骤 1 打开CodeBlocks新建工程:Shared library -- c -- sample [默认GUN GCC Compli ...
- 转:java.io.IOException: Exceeeded maximum number of redirects: 5 解决版本
Jmeter运行的时候出现的重定向超过n次的问题: When trying to test a Silverlight application, I get the below error. Has ...