前后端分离的必要

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

    /*设置select选中开始*/ var prod_type=$('.prod_type').val(); //alert(prod_type); var select = document.getE ...

  2. dubbo 的monitor监视器安装问题——————monitor一直处于正在启动状态

    一台服务器安装完zookeeper并启动后,然后在另一服务器安装monitor     dubbo-monitor-simple-2.8.3  解压安装 修改配置文件 dubbo.container= ...

  3. git 恢复丢失的文件-- 不提交入口文件

    务必进入当前controller下面,才能恢复 git checkout HEAD TestController.class.php 01备份index.php文件 02使用 小乌龟的git 删除 t ...

  4. 8、Spring+Struts2+MyBaits(Spring注解+jdbc属性文件+log4j属性文件)

    一.注解理论 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base- ...

  5. IOS CrackMe 破解学习

    一直在看别人如何破解一个app,下面自己也尝试着学习怎么去破解一个app的密码,下面是完整的过程. 准备工作: 一台mac或者pc安装了ssh客户端 一台越狱的iphone iphone上安装了ope ...

  6. Tomcat基础

    一.Tomcat体系结构 tomcat应该是java程序员最熟悉的servlet容器了,作为一个用java实现的web应用程序服务器,下图是tomcat的体系结构 一个常见的Tomcat配置文件以下x ...

  7. Kettle jdbc连接hive出现问题

    jdbc连接时报如下错误: Error connecting to database [k] : org.pentaho.di.core.exception.KettleDatabaseExcepti ...

  8. 抛弃阿里云,中国用户购买海外VPS的五个理由

    王掌柜在过去的五年多时间里,折腾过不少vps品牌,最开始玩的是一年一百多块钱的香港虚拟主机,后来业务量大了,开始折腾国内的小鸟云.阿里云.腾讯云.电信云.百度云主机,国外的linode\interse ...

  9. 小米2s刷机

    每次系统内存不足,卡的不行就恨不得马上换新手机,发现手机也没有什么大的毛病,也没有其他苛刻的要求. 换个新系统继续使用吧,除了屏幕小了一点,将就了吧.物尽其责,坚决抵制过度消费. 小米手机2s 16G ...

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

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