前后端分离的必要

  • 现在的趋势发展,需要把前后端开发和部署做到真正的分离
  • 做前端的谁也不想用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

其他关联代码

源码地址

https://github.com/je-ge/spring-boot

如果觉得我的文章对您有帮助,请予以打赏。您的支持将鼓励我继续创作!谢谢!



Spring Boot 系列教程9-swagger-前后端分离后的标准的更多相关文章

  1. 手把手教你使用 Spring Boot 3 开发上线一个前后端分离的生产级系统(一) - 介绍

    项目简介 novel 是一套基于时下最新 Java 技术栈 Spring Boot 3 + Vue 3 开发的前后端分离的学习型小说项目,配备详细的项目教程手把手教你从零开始开发上线一个生产级别的 J ...

  2. Spring Boot Security JWT 整合实现前后端分离认证示例

    前面两章节我们介绍了 Spring Boot Security 快速入门 和 Spring Boot JWT 快速入门,本章节使用 JWT 和 Spring Boot Security 构件一个前后端 ...

  3. Swagger - 前后端分离后的契约

    前后端分离 按照现在的趋势,前后端分离几乎已经是业界对开发和部署方式所达成的一种共识.所谓的前后端分离,并不是传统行业中的按部门划分,一部分人只做前端(HTML/CSS/JavaScript等等),另 ...

  4. 全栈的自我修养: 001环境搭建 (使用Vue,Spring Boot,Flask,Django 完成Vue前后端分离开发)

    全栈的自我修养: 环境搭建 Not all those who wander are lost. 彷徨者并非都迷失方向. Table of Contents @ 目录 前言 环境准备 nodejs v ...

  5. Spring Boot 系列教程15-页面国际化

    internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...

  6. Spring Boot 系列教程11-html页面解析-jsoup

    需求 需要对一个页面进行数据抓取,并导出doc文档 html解析器 jsoup 可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操 ...

  7. Spring Boot 系列教程7-EasyUI-datagrid

    jQueryEasyUI jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要 ...

  8. Spring Boot 系列教程19-后台验证-Hibernate Validation

    后台验证 开发项目过程中,后台在很多地方需要进行校验操作,比如:前台表单提交,调用系统接口,数据传输等.而现在多数项目都采用MVC分层式设计,每层都需要进行相应地校验. 针对这个问题, JCP 出台一 ...

  9. Spring Boot 系列教程18-itext导出pdf下载

    Java操作pdf框架 iText是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有很好 ...

随机推荐

  1. POJ 2054 Color a Tree#贪心(难,好题)

    题目链接 代码借鉴此博:http://www.cnblogs.com/vongang/archive/2011/08/19/2146070.html 其中关于max{c[fa]/t[fa]}贪心原则, ...

  2. Linux中kettle连接hadoop并传数据(5)

    http://wiki.pentaho.com/display/BAD/Loading+Data+into+HDFS 新建job

  3. kettle 连接Hadoop

    http://wiki.pentaho.com/display/BAD/Additional+Configuration+for+YARN+Shims Copy *-site.xml Cluster ...

  4. delphi 中sql的语法规范

    1.引号配对:  这是在Delphi使用SQL语句时容易出错的地方,由于delphi规定在字符串中用两个西文的单引号“''”表示一个“'”,在拼装语句的时候就容易疏忽遗漏.  Delphi里有个函数Q ...

  5. html5 让IE6,7支持HTML5语义化标签的文件

    https://github.com/aFarkas/html5shiv/blob/master/src/html5shiv.js   只要应用这个js就行了

  6. 洛谷-A+B Problem-洛谷的第一个任务

    题目描述 Description 输入两个整数a,b,输出它们的和(a,b<=10^9)  输入输出格式 Input/output 输入格式:两个整数以空格分开输出格式:一个数  输入输出样例  ...

  7. 一把刀终极配置 For XP v2.0 免费绿色版

    软件名称: 一把刀终极配置 For XP 软件语言: 简体中文 授权方式: 免费软件 运行环境: WinXP 软件大小: 924KB 图片预览: 软件简介: 一把刀终极配置 For XP,用于快速方便 ...

  8. thinkPHP框架学习笔记

    class ZhuantiAction extends Action { public function index() { $name = trim($this->_get('name')); ...

  9. python_变量的命名规则

    python 变量的命名规则: 1. 要具有描述性 2.变量名只能由 数字,字母 ,下划线 组成,不可以是空格或者特殊字符(#!%……&) 3.不能以数字开头 4.保留字符不可用(print ...

  10. Java heap space

    //首先检查程序有没有限入死循环 这个问题主要还是由这个问题 java.lang.OutOfMemoryError: Java heap space 引起的.第一次出现这样的的问题以后,引发了其他的问 ...