zoukankan      html  css  js  c++  java
  • spring boot 使用spring.resources.static-locations 分离系统模版&&资源文件

    方便我们将资源配置以及模版&&静态文件分离出来,而不是打包在一起,比如以下的一个demo

    参考配置:

    server.port=8006
    spring.application.name=appdemo
    spring.resources.static-locations=pdf
    spring.resources.chain.cache=false

    maven 配置

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
    
       <groupId>com.dalong</groupId>
       <artifactId>productapp</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>jar</packaging>
    
       <name>productapp</name>
       <description>Demo project for Spring Boot</description>
    
       <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.0.2.RELEASE</version>
          <relativePath/> <!-- lookup parent from repository -->
       </parent>
    
       <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
          <java.version>1.8</java.version>
       </properties>
    
       <dependencies>
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
    
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
          </dependency>
          <dependency>
             <groupId>org.springframework.restdocs</groupId>
             <artifactId>spring-restdocs-mockmvc</artifactId>
             <scope>test</scope>
          </dependency>
       </dependencies>
    
       <build>
          <plugins>
             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
          </plugins>
          <!--核心进行需要处理的资源文件copy 而不是直接打包到jar 包中,方便我们进行修改 -->
          <resources>
             <resource>
                <directory>src/main/resources/pdf/</directory>
                <filtering>false</filtering>
                <targetPath>${basedir}/target/pdf</targetPath>
             </resource>
             <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                   <include>application.properties</include>
                </includes>
             </resource>
          </resources>
       </build>
    
    
    </project>
    

    代码使用

    ```code
    package com.dalong.productapp.com.dalong.productapp.controllers;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.util.HashMap;
    import java.util.Map;
    @RestController
    public class ProductService {
    @RequestMapping(value = "/productionStream")
    public Object productsList() throws IOException {

         Map<String,String> products=new HashMap<>();
         products.put("name","appdemo");
         products.put("amount","10");
         File file = new File("pdf/myinfo.txt");
         products.put("myfile",file.getCanonicalPath());
         return products;
    }
    

    }

    ```

    项目结构

    构建生成结果

    classes 目录

    参考资料

    https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

  • 相关阅读:
    [HDU6793] Tokitsukaze and Colorful Tree
    [NOI2020]命运
    [NOI2020]美食家
    模拟9
    晚测2
    模拟8
    联考4
    模拟7
    模拟6
    关于数论
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/9069680.html
Copyright © 2011-2022 走看看