SpringBoot系统列 5 - 接口版本控制、SpringBoot FreeMarker模板引擎

2023-11-16

 

接着上篇博客的代码继续写

1.接口版本控制

一个系统上线后会不断迭代更新,需求也会不断变化,有可能接口的参数也会发生变化,如果在原有的参数上直接修改,可能会影响线上系统的正常运行,这时我们就需要设置不同的版本,这样即使参数发生变化,由于老版本没有变化,因此不会影响上线系统的运行。

一般我们可以在地址上带上版本号,也可以在参数上带上版本号,还可以再 header 里带上版本号,这里我们在地址上带上版本号,大致的地址如:http://api.example.com/v1/test,其中,v1 即代表的是版本号。具体做法请看代码:

import org.springframework.web.bind.annotation.Mapping;

import java.lang.annotation.*;

/**
 * 版本控制
 * @author XIHONGLEI
 * @date 2018-11-15
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface ApiVersion {
    int value();
}
import org.springframework.web.servlet.mvc.condition.RequestCondition;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author XIHONGLEI
 * @date 2018-11-15
 */
public class ApiVersionCondition implements RequestCondition<ApiVersionCondition> {
    // 路径中版本的前缀, 这里用 /v[1-9]/的形式
    private final static Pattern VERSION_PREFIX_PATTERN = Pattern.compile("v(\\d+)/");
    private int apiVersion;

    public ApiVersionCondition(int apiVersion) {
        this.apiVersion = apiVersion;
    }

    @Override
    public ApiVersionCondition combine(ApiVersionCondition other) {
        // 采用最后定义优先原则,则方法上的定义覆盖类上面的定义
        return new ApiVersionCondition(other.getApiVersion());
    }

    @Override
    public ApiVersionCondition getMatchingCondition(HttpServletRequest request) {
        Matcher m = VERSION_PREFIX_PATTERN.matcher(request.getRequestURI());
        if (m.find()) {
            Integer version = Integer.valueOf(m.group(1));
            if (version >= this.apiVersion) {
                return this;
            }
        }
        return null;
    }

    @Override
    public int compareTo(ApiVersionCondition other, HttpServletRequest request) {
        // 优先匹配最新的版本号
        return other.getApiVersion() - this.apiVersion;
    }

    public int getApiVersion() {
        return apiVersion;
    }
}
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;

/**
 * @author XIHONGLEI
 * @date 2018-11-15
 */
public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
    @Override
    protected RequestCondition<ApiVersionCondition> getCustomTypeCondition(Class<?> handlerType) {
        ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);
        return createCondition(apiVersion);
    }

    @Override
    protected RequestCondition<ApiVersionCondition> getCustomMethodCondition(Method method) {
        ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);
        return createCondition(apiVersion);
    }

    private RequestCondition<ApiVersionCondition> createCondition(ApiVersion apiVersion) {
        return apiVersion == null ? null : new ApiVersionCondition(apiVersion.value());
    }
}
#然后在WebConfig配置类中注入Bean

import com.hello.config.CustomRequestMappingHandlerMapping;
import com.hello.filter.ApiInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * 配置类
 * @author XIHONGLEI
 * @date 2018-10-31
 */
@SpringBootConfiguration
public class WebConfig extends WebMvcConfigurationSupport {

   

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        // 将 ApiInterceptor 拦截器类添加进去
        registry.addInterceptor(new ApiInterceptor());
    }

   

    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandlerMapping();
        handlerMapping.setOrder(0);
        handlerMapping.setInterceptors(getInterceptors());
        return handlerMapping;
    }
}
#最后定义一个带版本控制的接口
import com.hello.WebConfig;
import com.hello.config.ApiVersion;
import com.hello.entity.ContractDetailDto;
import com.hello.service.CheckPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class HelloController {

    @Autowired
    private WebConfig webConfig;

    @ApiVersion(1)
    @RequestMapping("{version}/getName")
    public String vGetName() {
        return "Hello World! version 1";
    }
    @ApiVersion(2)
    @RequestMapping("{version}/getName")
    public String getName() {
        return "Hello World! version 2";
    }
}

查看效果:

 

2.模板引擎

在传统的 SpringMVC 架构中,我们一般将 JSP、HTML 页面放到 webapps 目录下面,但是 Spring Boot 没有 webapps,更没有 web.xml,如果我们要写界面的话,该如何做呢?

Spring Boot 官方提供了几种模板引擎:FreeMarker、Velocity、Thymeleaf、Groovy、mustache、JSP。

这里以 FreeMarker 为例讲解 Spring Boot 的使用。

首先引入 FreeMarker 依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

在 resources 下面建立两个目录:static 和 templates,如图所示:

其中 static 目录用于存放静态资源,譬如:CSS、JS、HTML 等,templates 目录存放模板引擎文件,我们可以在 templates 下面创建一个文件:index.ftl(freemarker 默认后缀为 .ftl),并添加内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello ${name}!</title>
</head>
<body>
        Hello ${name}!
</body>
</html>

然后在POM中配置Resource的时候一定要把所有的资源文件都包括:

<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

新建Contrlller:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/index");
        view.addObject("name","Tom");
        return view;
    }
}

看结果:

 

转载于:https://www.cnblogs.com/raphael5200/p/9968057.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot系统列 5 - 接口版本控制、SpringBoot FreeMarker模板引擎 的相关文章

随机推荐

  • 概率论基础(sigma域、测度)

    一 样本空间 事件 样本空间 Omega 指一个实验的可能结果的集合 omega in Omega 称为 样本
  • 定时任务及分布式定时任务注意事项

    一 定时任务默认是阻塞的 定时任务默认是阻塞的 即串行执行 若一个服务配置多个定时任务 需要等上一个定时任务执行完 才能执行下一个定时任务 一个定时任务超长了 也不应该阻塞其他定时任务的执行 如一个定时任务每秒执行 而业务执行时间是5秒 那
  • vscode (1)直接编译

    第一步创建c文件 第二步配置 终端 配置默认生成任务 选择 usr bin gcc version 2 0 0 tasks type cppbuild label C C gcc 生成活动文件 command usr bin gcc 使用的
  • Stacked Queries(堆叠注入)

    文章目录 基本知识 原理介绍 堆叠注入的局限性 Mysql数据库实例介绍 CTF 实战与各种姿势 修改表名 利用HANDLER语句 利用MySql预处理 正常利用 MySql预处理配合十六进制绕过关键字 MySql预处理配合字符串拼接绕过关
  • jax安装

    Windows 安装 pip install jaxlib 0 3 5 f https whls blob core windows net unstable index html pip install jaxlib cuda111 f
  • Android开发工作中遇到的重点和难点总结

    1 Android N floating widget无法显示 统一管理一个window token解决了此问题 2 Pop up window在Android6 0上出现花屏 3 由于状态栏的影响 悬浮窗上下跳动 4 Wi Fi安全的数据
  • Windows下的oracle 11g的入门

    图全部都挂了 写的太累了 有空再来更 几个月没用oracle之后 花了一个下午把oracle的基本操作迅速捡回来了 记录如下 一 安装oracle11G 1 1 首先要下载oracle服务端和客户端 官网下载链接如下 http www or
  • content-type的几种取值

    目录 Content Type的几种取值 1 text plain 2 text html 3 application json 4 application xml 5 image jpeg 6 image png 7 audio mpeg
  • Socket编程基础

    提示 文章写完后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 前言 一 基于TCP的socket通信流程 二 基于UDP的socket通信流程 三 TCP协议下socket编程主要API接口介绍 1 int socket in
  • 【Java】迭代器之:Iterable & Iterator

    在我们Java中的迭代器是一个接口 名为Iterator 他有两个抽象方法 hasNext方法 用来判断还有没有数据访问 next方法 用来访问集合的下一个数据 迭代器可以访问不同特性的集合数据 而无需关心他们的内部实现 注意 集合并不是直
  • 小学奥数题使用python解决(使用2倒9中不重复的数使得{}+{}={}{}-{}=1{}满足)

    使用2 9中不重复的数使得 1 满足 样子不太好看 1 利用for循环和if语句 代码 利用2 9不重复的数使得 1 i 0 for a1 in range 2 10 for a2 in range 2 10 if a1 a2 and a1
  • 新学期阅读计划

    1 再认真阅读 设计模式之禅 在理解的基础上应用设计模式 2 编程之美 共4章 61个有意思的题目 3 图书馆借阅 算法导论 4 再阅读 算法之道 5 了解 操作系统导论 真正理解不要死记硬背 6 反复多次阅读经典的论文 特别是及时和师姐多
  • 部署篇-Zabbix中文乱码字符集的修正

    部署zabbix监控后默认是英文 默认不支持中文字符集 切换成中文后会出现以下情况 解决方案 从Window服务器找到相应的字休复制到zabbix Server服务器上 控制面板 字体 选择一种中文字体 建议simkai ttf root
  • Java堆和栈应用实例

    以下是一个简单的Java程序 演示了Java堆和栈的应用实例 public class HeapAndStackExample public static void main String args 创建一个对象并分配在堆内存中 Perso
  • CTFshow web入门---web56

    CTFshow web入门 web56 题目 题目分析 查看本题 发现本题为命令执行类题目 但是有一个很致命的点 那么就是他过滤了所有的字母和数字 以及一系列的符号 因此本题最值得推敲的点就是如何实现无字母数字的命令执行 通过拜读P神的一篇
  • 关系型数据库RDBMS -MySQL基础入门(三)数据分片

    数据分片 相关概念 分库分表 分片 存在一台数据库的服务器中的数据 特定方式拆分 分散存放在多台数据库服务中 达到单台服务器负载的效果 垂直分割 纵向切分 按业务类型 什么是垂直分割 纵向切分 把单一的表 拆分成多个表 并分散到不同的数据库
  • 深入理解gtest C/C++单元测试经验谈

    Google C Testing Framework 简称gtest http code google com p googletest 是Google公司发布的一个开源C C 单元测试框架 已被应用于多个开源项目及Google内部项目中
  • spring Data JPA 拾遗

    Preface JPA在国内的使用频率较小 但也是一个值得学习的极为优秀的ORM框架 DDD的思想在里面体现得淋漓尽致 结构图 配置 1 2 3 4 5 6 7 8 9 10 11 spring jpa generate ddl false
  • 搭建jboss

    jboss 是中间件comcat是框架 jboss 基于java需要安装jbk配置环境变量 配置环境变量 我的电脑 右键 属性 高级 环境变量 新建系统变量 变量名为 JAVA HOME 变量值 C Program Files Java j
  • SpringBoot系统列 5 - 接口版本控制、SpringBoot FreeMarker模板引擎

    接着上篇博客的代码继续写 1 接口版本控制 一个系统上线后会不断迭代更新 需求也会不断变化 有可能接口的参数也会发生变化 如果在原有的参数上直接修改 可能会影响线上系统的正常运行 这时我们就需要设置不同的版本 这样即使参数发生变化 由于老版