前后端分离的必要

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

    Configuring Babel 6 Setting up ES6

  2. CDOJ 1270 Playfair(模拟)

    题目链接 Playfair is a kind of substitution cipher.And the encryption role is simple.In general,there ar ...

  3. webapi中使用Route标签

    Prior to Web API 2, the Web API project templates generated code like this: protected void Applicati ...

  4. CodeForces 697B Barnicle 模拟

    强行模拟 纪念一下…… #include<stdio.h> #include<iostream> #include<algorithm> #include<m ...

  5. Python 学习笔记5

    Life is like a box of chocolate. 今天继续学习Python数据结构. http://www.pythondoc.com/pythontutorial3/datastru ...

  6. POJ 3347 Kadj Squares (线段覆盖)

    题目大意:给你几个正方形的边长,正方一个顶点在x轴上然后边与x轴的夹角为45度,每个正方形都是紧贴的,问从上面看能看的正方形的编号 题目思路:线段覆盖,边长乘上2防止产生小数,求出每个正方形与x轴平行 ...

  7. 8. Python自定义模块humansize

    我们在提取一个文件元信息的时候,经常会使用到获取元信息的size, 但是默认提取出来的是字节为单位计算的大小,我们需要转换成MB或者GB 或者TB的大小. 因此就需要使用到humansize这个模块, ...

  8. nmon的安装与使用

    nmon的安装与使用 1.下载 nmon:http://nmon.sourceforge.net/pmwiki.php?n=Site.Download nmonanalyser http://www. ...

  9. leetcode83,删除有序链表中的重复元素

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  10. runat="server" 是什么意思?

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...