上次说过springboot其实就是一个CI工具,如何体验出来CI的作用就是持续集成,它可以集成各种的工具,这里说说关于模板的集成引擎和Swagger。

(一)Spring boot 集成模板引擎实现web应用

  • 静态资源访问

静态资源

js, css, html, 图片,音视频

静态资源路径

系统可以直接访问的路径,且路径下的所有文件均可被用户直接读取。
Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
/static,/public,/resources,/META-INF/resources

在classpath下面创建static目录,并且加入一个图片a.png

加入之后,然后不需要重启直接访问:http://localhost:8888/a.jpg

properties 内修改默认的静态资源目录

spring.resources.static-locations

(二)集成模板引擎

Spring Boot强烈建议使用模板引擎渲染html页面,避免使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性。Thymeleaf(spring boot推荐), FreeMarker。

  • Thymeleaf

Spring boot默认的模板配置路径为:src/main/resources/templates。当然也可以修改这个路径,通过配置文件的属性,这个在上次的配置的文件的里面有详细的解释配置里面有。

集成Thymeleaf步骤

1.修改pom.xml, 增加如下依赖。

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

2.编写Controller

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;/*** @program: springboot3d* @description: ${description}* @author: LiMing* @create: 2019-06-09 09:15**/
@Controller
public class SampleController {@RequestMapping("/testThymeleaf")public String testThymeleaf(ModelMap map) {// 设置属性map.addAttribute("name", "idig8");// testThymeleaf:为模板文件的名称// 对应src/main/resources/templates/testThymeleaf.htmlreturn "testThymeleaf";}
}

3.在src/main/resources/下面建立templates/testThymeleaf.html

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en"><meta charset="UTF-8" /><title>testThymeleaf</title>
</head>
<body>
<h1 th:text="${name}">idig88</h1>
</body>
</html>

4.运行spring boot,浏览器输入:http://localhost:8888/testThymeleaf

  • FreeMarker
    1.修改pom.xml,增加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2.Controller

@RequestMapping("/testFreemarker")public String testFreemarker(Map<String,String> map) {map.put("name", "张三");return "hello"; //默认为src/main/resources/templates/hello.flt}

3.hello.flt,目录为:src\main\resources\templates

<html>
<body>hello, ${name}
</body>
</html>

4.运行spring boot main,浏览器输入如下地址:http://localhost:8881/testFreemarker

(二)集成Swagger2构建RESTful API文档

  • Swagger2
    1.随项目自动生成强大RESTful API文档,减少工作量
    2.API文档与代码整合在一起,便于同步更新API说明
    3.页面测试功能来调试每个RESTful API

  • 集成Swagger2步骤
    1.修改pom.xml, 添加Swagger2依赖

<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>

2.创建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;/*** @program: springboot3d* @description: ${description}* @author: LiMing* @create: 2019-06-09 10:20**/
@Configuration
@EnableSwagger2
public class SwaggerConfig {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.idig8.springboot"))// 指定扫描包下面的注解.paths(PathSelectors.any()).build();}// 创建api的基本信息private ApiInfo apiInfo() {return new ApiInfoBuilder().title("集成Swagger2构建RESTful APIs").description("集成Swagger2构建RESTful APIs").termsOfServiceUrl("https://www.idig8.com").contact("欢迎关注:编程坑太多").version("1.0.0").build();}
}

2.创建Controller: SwaggerController.java

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;/*** @program: springboot3d* @description: ${description}* @author: LiMing* @create: 2019-06-09 10:22**/
@RestController
@RequestMapping(value="/swagger")
public class SwaggerController {@ApiOperation(value="获取用户信息", notes="根据id来获取用户详细信息")@ApiImplicitParam(name="id", value="用户ID", required=true, dataType="String")@RequestMapping(value="/{id}", method= RequestMethod.GET)public Map<String,String> getInfo(@PathVariable String id) {Map<String ,String> map = new HashMap<String, String>();map.put("name", "张三");map.put("age", "34");return map;}
}

4.启动Spring boot,访问Swagger UI界面:http://localhost:8881/swagger-ui.html

PS:今天说了简单模板引擎和swagger2的介绍,只是功能介绍详细的细节没有做阐述。先从会用开始吧,具体的细节还是看官方的api更详细,这里只是从入门开始说起。

软件架构-Spring boot集成模板引擎swagger2实现相关推荐

  1. Spring Boot整合模板引擎jsp

    为什么80%的码农都做不了架构师?>>>    jsp也算是一种模板引擎吧.整合jsp前,先说一下运行SpringBoot项目的几种方式 1. 运行SpringBoot项目的几种方式 ...

  2. Spring Boot 使用模板引擎

    Spring Boot 推荐使用Thymeleaf.FreeMarker.Velocity.Groovy.Mustache等模板引擎.不建议使用JSP. Spring Boot 对以上几种引擎提供了良 ...

  3. Spring Boot 9-FreeMarker模板引擎

    springboot使用freemarker模板引擎 首先引入freemarker <!-- freemarker依赖包 --> <dependency><groupId ...

  4. Spring Boot + Drools+模板引擎,优惠券的动态添加以及购物车结算

    目录 二.项目文件结构以及数据库表结构 三.主要项目文件说明 1.pom文件 2.配置文件 3.实体类 4.添加优惠券业务实现 5.购物车结算业务实现 四.实现效果 1.添加优惠券 2.购物车结算 五 ...

  5. Spring Boot - Thymeleaf模板简介以及集成

    文章目录 Spring Boot - Thymeleaf模板简介以及集成 1.什么是Thymeleaf? 2.标准表达式 2.1 变量表达式 2.2 选择表达式/星号表达式 2.3 URL表达式 2. ...

  6. Spring Boot 集成SnakerFlow流程引擎,简介、功能列表、详细解读、扩展点分析

    文章目录 简介 功能列表 流程定义 任务参与者 参与者设置 动态添加.删除参与者 组支持 详细解读 Spring Boot集成 表定义 表详细说明: 字段详细说明: 常见操作 常规API 综合查询 模 ...

  7. 从零搭建开发脚手架 Spring Boot集成Mybatis-plus之一

    文章目录 简介 特性 框架结构 依赖集成 依赖 配置 编码 开始使用 核心功能 代码生成器 添加依赖 编码 编写配置 自定义模板引擎 自定义代码模板 自定义属性注入 字段其他信息查询注入 实战总结 常 ...

  8. spring boot 集成 websocket 实现消息主动推送

    前言 http协议是无状态协议,每次请求都不知道前面发生了什么,而且只可以由浏览器端请求服务器端,而不能由服务器去主动通知浏览器端,是单向的,在很多场景就不适合,比如实时的推送,消息通知或者股票等信息 ...

  9. SpringMVC访问WEB-INF下的jsp解决方案Spring Boot集成使用jsp

    SpringMVC访问WEB-INF下的jsp解决方案 一. 问题 ​将项目中用到的jsp等文件放在WEB-INF目录下.实际开发过程中,需要在框架页面通过iframe嵌入对应的具体页面,此处如果直接 ...

最新文章

  1. 如何在Matlab中获取函数参数的数目?
  2. Java™ 教程(字符流)
  3. 运行SSIS包的几种方式
  4. shiro教程:整合ehcache缓存
  5. 【知识星球】ElementAI提出超复杂多尺度细粒度图像分类Attention模型
  6. Http Handler 介绍(转)
  7. textarea换行符转换
  8. script标签属性用type还是language?
  9. Android中SimpleAdapter的使用—自定义列表
  10. oracle的aud文件,oracle asm实例的aud文件有关问题
  11. waring Release版本
  12. mongo 主从数据不同步
  13. [BZOJ4811][YNOI2017]由乃的OJ(树链剖分+线段树)
  14. 【实用】CSS Border使用小分享——盒模型
  15. Centos7 压缩文件
  16. penghui_031413 Bat命令学习
  17. 【Windows逆向】Windows进程动态patch入门+pyqt5搭建GUI
  18. ubuntu 16.04 下安装搜狗输入法教程
  19. Microsoft OLE DB Provider for SQL Server 错误 ‘80004005‘错误原因和解决方案
  20. 前沿|十位顶级大咖为您把脉容器技术大势

热门文章

  1. 中文与 \u5927\u732a\u8e44\u5b50 这一类编码互转
  2. 2021年安全员-C证试题及解析及安全员-C证作业模拟考试
  3. 火狐浏览器和ie,谷歌的兼容问题
  4. vr相关文章第一弹,屏幕分辨率和屏幕ppi的区别
  5. 游戏测试用例设计实例
  6. vagrant up下载box慢的解决办法
  7. 串口通信协议和Linux下的串口编程
  8. 巧妇能为少米之炊(2)——幽灵侩子手(LMK)
  9. python基础:案例:学生名片信息管理系统
  10. 净利润同比下降20%,宜人贷能熬过P2P寒冬吗?