方便咱們將資源配置以及模版&&靜態文件分離出來,而不是打包在一塊兒,好比如下的一個demohtml
參考配置:
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;java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;web
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 {spring
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; }
}apache
```mvc
項目結構
app
構建生成結果
classes 目錄maven
參考資料
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.htmlspring-boot