在 Spring Boot 中提供静态内容
最后修改时间:2023年7月20日
Spring Boot 静态内容展示了如何在 Spring Boot 应用程序中提供静态内容。
Spring 是一个流行的 Java 应用程序框架。Spring Boot 旨在以最小的努力创建独立的、生产级的基于 Spring 的应用程序。
Spring Boot 会自动添加位于以下任何目录中的静态 Web 资源
- /META-INF/resources/
- /resources/
- /static/
- /public/
这些目录位于类路径或 ServletContext
的根目录下。
在我们的应用程序中,我们有一个 HTML 文件,其中包含一个简单的链接。该链接触发了 Web Boot 应用程序的响应。它返回一条纯文本消息。
build.gradle ... src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ ├───controller │ │ │ MyController.java │ │ └───model │ │ Message.java │ └───resources │ │ application.properties │ └───static │ │ index.html │ └───css │ main.css └───test └───java └───com └───zetcode └───controller MyControllerTest.java
这是 Spring Boot 应用程序的项目结构。
plugins { id 'org.springframework.boot' version '3.1.1' id 'io.spring.dependency-management' version '1.1.0' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { useJUnitPlatform() }
这是 gradle 构建文件。spring-boot-starter-web
是使用 Spring MVC 构建 Web 应用程序的启动器。spring-boot-starter-test
导入必要的测试模块。
package com.zetcode.model; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Message { @Value("${app.message}") private String message; public String get() { return message; } }
Message
是一个模型类,它包含一个简单的文本消息给客户端。
@Value("${app.message}") private String message;
我们将来自 application.properties
的一个值注入到 message
变量中。
app.message=Hello there
application.properties
文件包含 Spring Boot 应用程序的各种配置设置。我们定义一个自定义属性,其中包含一个文本消息。
package com.zetcode.controller; import com.zetcode.model.Message; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyController { private final Message message; public MyController(Message message) { this.message = message; } @GetMapping(path = "/message") @ResponseBody public String message() { return message.get(); } }
这是 Spring Boot Web 应用程序的控制器类。控制器使用 @Controller
注解进行修饰。控制器有一个映射;它映射到 /message
路径并返回一条纯文本消息。
private final Message message; public MyController(Message message) { this.message = message; }
一个 Message
对象被注入到该属性中。
@GetMapping(path = "/message") @ResponseBody public String message() { return message.get(); }
message
方法响应 GET 请求。@ResponseBody
注解将字符串值放入 Web 响应体中。
<!DOCTYPE html> <html> <head> <title>Home page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="css/main.css" rel="stylesheet" type="text/css"> </head> <body> <h2>Home page</h2> <a href="/message">Get message</a> </body> </html>
在 index.html
文件中,我们有一个链接,它会调用来自 Web 应用程序的响应。该文件位于 src/main/resources/static
目录中,该目录是 Spring 查找静态内容的默认目录。
<link href="css/main.css" rel="stylesheet" type="text/css">
在链接标签中,我们引用了 main.css
静态资源,它位于 src/main/resources/static/css
目录中。
h2 { color: blue }
在 main.css
文件中,我们将 h2
标签设置为蓝色。
package com.zetcode.controller; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc public class MyControllerTest { @Autowired private MockMvc mockMvc; @Test public void getHome() throws Exception { this.mockMvc.perform(get("/")) .andDo(print()) .andExpect(status().isOk()) .andExpect(forwardedUrl("index.html")); } @Test public void getMessage() throws Exception { this.mockMvc.perform(get("/message")) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string("Hello there")); } }
在 MyControllerTest
中,我们有两个测试:一个用于主页,一个用于返回的消息文本。
package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
`Application` 设置了 Spring Boot 应用程序。
在本文中,我们已经在 Spring Boot 应用程序中提供了静态内容。