前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >『互联网架构』软件架构-Spring boot集成模板引擎swagger2实现(87)

『互联网架构』软件架构-Spring boot集成模板引擎swagger2实现(87)

作者头像
IT架构圈
发布2019-06-25 11:42:55
4440
发布2019-06-25 11:42:55
举报
文章被收录于专栏:IT架构圈IT架构圈

上次说过springboot其实就是一个CI工具,如何体验出来CI的作用就是持续集成,它可以集成各种的工具,这里说说关于模板的集成引擎和Swagger。源码:https://github.com/limingios/netFuture/tree/master/源码/『互联网架构』软件架构-Spring boot集成模板引擎实现(86)/

(一)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 内修改默认的静态资源目录

代码语言:javascript
复制
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, 增加如下依赖。

代码语言:javascript
复制
    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-thymeleaf</artifactId>    </dependency>

2.编写Controller

代码语言:javascript
复制
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 **/@Controllerpublic class SampleController {
    @RequestMapping("/testThymeleaf")    public String testThymeleaf(ModelMap map) {        // 设置属性        map.addAttribute("name", "idig8");        // testThymeleaf:为模板文件的名称        // 对应src/main/resources/templates/testThymeleaf.html        return "testThymeleaf";    }}

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

代码语言:javascript
复制
<!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,增加依赖
代码语言:javascript
复制
<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-freemarker</artifactId></dependency>

2.Controller

代码语言:javascript
复制
@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

代码语言:javascript
复制
<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依赖
代码语言:javascript
复制
<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配置类

代码语言:javascript
复制
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@EnableSwagger2public class SwaggerConfig {    @Bean    public 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

代码语言:javascript
复制
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更详细,这里只是从入门开始说起。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程坑太多 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • (一)Spring boot 集成模板引擎实现web应用
  • (二)集成模板引擎
  • (二)集成Swagger2构建RESTful API文档
相关产品与服务
Serverless HTTP 服务
Serverless HTTP 服务基于腾讯云 API 网关 和 Web Cloud Function(以下简称“Web Function”)建站云函数(云函数的一种类型)的产品能力,可以支持各种类型的 HTTP 服务开发,实现了 Serverless 与 Web 服务最优雅的结合。用户可以快速构建 Web 原生框架,把本地的 Express、Koa、Nextjs、Nuxtjs 等框架项目快速迁移到云端,同时也支持 Wordpress、Discuz Q 等现有应用模版一键快速创建。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档