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

栏目: Java · 发布时间: 4年前

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

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

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

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

  • 静态资源访问

    >静态资源

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

静态资源路径

系统可以直接访问的路径,且路径下的所有文件均可被用户直接读取。

Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:

/static,/public,/resources,/META-INF/resources

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

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

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

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

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

spring.resources.static-locations

(二)集成模板引擎

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

  • Thymeleaf

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

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

集成Thymeleaf步骤

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

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

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

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.html
        return "testThymeleaf";
    }
}

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

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>

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

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

  • FreeMarker

    1.修改pom.xml,增加依赖

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

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

2.Controller

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

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

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

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

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

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>

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

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 {
    @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();
    }
}

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

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

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

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

百度未收录

>>原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!

>>原文链接地址:上一篇:

已是最新文章


以上所述就是小编给大家介绍的《『互联网架构』软件架构-Spring boot集成模板引擎swagger2实现(87)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

iOS软件开发揭密

iOS软件开发揭密

虞斌 / 电子工业出版社 / 2011-5-1 / 79.00元

本书以严密的体系性提供了iPhone和iPad软件开发从入门到专家的系统性知识,并提供来源于真实项目的可重用商业代码。书中的每个实例都是项目经验的提炼,深入浅出地讲解iPhone和iPad软件开发的核心技术要点,基本涵盖了iOS软件开发在真实商业项目中所需要的所有主题,并将实例介绍的技术深度和超值的实用性结合在一起,成为本书的特色。 随书附赠的光盘中包含了书中大量案例的完整工程源代码,可以让......一起来看看 《iOS软件开发揭密》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换