refactor: demo/app-simple 更名为 demo/app
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package cn.odboy.demo;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.net.InetAddress;
|
||||
import java.time.Duration;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import jakarta.servlet.ServletContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.SpringVersion;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 运行时信息接口。与 demo/app-simple/springboot-jar17 里的同名类保持一致的输出结构,方便对比 jar 与 war 两种打包方式。
|
||||
*/
|
||||
@RestController
|
||||
public class DemoController {
|
||||
|
||||
private static final ZoneId TZ8 = ZoneId.of("Asia/Shanghai");
|
||||
private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss XXX");
|
||||
|
||||
@Autowired(required = false)
|
||||
private ServletContext servletContext;
|
||||
|
||||
@GetMapping({"/", "/info"})
|
||||
public Map<String, Object> info() {
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
long uptimeMillis = ManagementFactory.getRuntimeMXBean().getUptime();
|
||||
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("message", "spring-war17 is running");
|
||||
body.put("java", System.getProperty("java.version"));
|
||||
body.put("javaHome", System.getProperty("java.home"));
|
||||
body.put("vm", System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version"));
|
||||
body.put("spring", SpringVersion.getVersion());
|
||||
body.put("application", "Spring MVC " + SpringVersion.getVersion() + " + " + servletContextName());
|
||||
body.put("os", System.getProperty("os.name") + " " + System.getProperty("os.arch"));
|
||||
body.put("hostname", hostname());
|
||||
body.put("contextPath", servletContext == null ? "" : servletContext.getContextPath());
|
||||
body.put("zoneId", ZoneId.systemDefault().getId());
|
||||
body.put("time", ZonedDateTime.now(TZ8).format(TIME_FMT));
|
||||
body.put("uptime", Duration.ofMillis(uptimeMillis).toString());
|
||||
body.put("memory", runtime.totalMemory() / 1024 / 1024 + "MB / max " + runtime.maxMemory() / 1024 / 1024 + "MB");
|
||||
return body;
|
||||
}
|
||||
|
||||
@GetMapping("/hello/{name}")
|
||||
public Map<String, Object> hello(@PathVariable String name) {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("message", "Hello, " + name + "!");
|
||||
body.put("time", ZonedDateTime.now(TZ8).format(TIME_FMT));
|
||||
return body;
|
||||
}
|
||||
|
||||
private String servletContextName() {
|
||||
if (servletContext == null) {
|
||||
return "Tomcat";
|
||||
}
|
||||
return servletContext.getServerInfo();
|
||||
}
|
||||
|
||||
private static String hostname() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostName();
|
||||
} catch (Exception e) {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cn.odboy.demo;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* 页面路由,模板见 src/main/resources/templates/index.html。
|
||||
*/
|
||||
@Controller
|
||||
public class PageController {
|
||||
|
||||
private final DemoController demoController;
|
||||
|
||||
public PageController(DemoController demoController) {
|
||||
this.demoController = demoController;
|
||||
}
|
||||
|
||||
/** 渲染 templates/index.html;/api/page 返回页面,其它 /api/* 仍返回 JSON */
|
||||
@GetMapping("/page")
|
||||
public String page(Model model) {
|
||||
Map<String, Object> info = demoController.info();
|
||||
model.addAttribute("info", info);
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.odboy.demo;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
/**
|
||||
* 替代 web.xml:Tomcat 10.1 启动时通过 Servlet 3+ 的 ServiceLoader 发现本类,
|
||||
* 由它注册 DispatcherServlet 与根容器。前缀 /api 的请求进 Spring MVC,其余请求交给 Tomcat 默认 Servlet。
|
||||
*/
|
||||
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class<?>[]{WebConfig.class};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[]{"/api/*"};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.odboy.demo;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.thymeleaf.spring6.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
||||
|
||||
/**
|
||||
* Spring MVC 配置。@EnableWebMvc 已注册包括 Jackson JSON 在内的默认消息转换器,
|
||||
* 这里只额外加上 Thymeleaf 视图解析。
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan("cn.odboy.demo")
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
|
||||
// 与 springboot-jar17 保持同一套模板路径:src/main/resources/templates/*.html
|
||||
resolver.setPrefix("templates/");
|
||||
resolver.setSuffix(".html");
|
||||
resolver.setTemplateMode("HTML");
|
||||
resolver.setCharacterEncoding("UTF-8");
|
||||
resolver.setCacheable(true);
|
||||
|
||||
SpringTemplateEngine engine = new SpringTemplateEngine();
|
||||
engine.setTemplateResolver(resolver);
|
||||
|
||||
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||
viewResolver.setTemplateEngine(engine);
|
||||
viewResolver.setCharacterEncoding("UTF-8");
|
||||
viewResolver.setOrder(1);
|
||||
registry.viewResolver(viewResolver);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>spring-war17 运行信息</title>
|
||||
<style>
|
||||
body { font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; margin: 40px; color: #222; }
|
||||
h1 { font-size: 20px; }
|
||||
table { border-collapse: collapse; min-width: 520px; }
|
||||
th, td { border: 1px solid #ddd; padding: 6px 12px; text-align: left; }
|
||||
th { background: #f5f5f5; width: 160px; }
|
||||
code { background: #f5f5f5; padding: 1px 4px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>spring-war17 运行信息</h1>
|
||||
<table>
|
||||
<tr th:each="entry : ${info}">
|
||||
<th th:text="${entry.key}">key</th>
|
||||
<td th:text="${entry.value}">value</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
其他接口:<code>api/info</code>、<code>api/hello/{name}</code>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
Tomcat 部署描述符,不影响 Spring 初始化。当前版本没有用 web.xml 写 servlet 映射,
|
||||
由 WebAppInitializer(ServletContainerInitializer)注册 DispatcherServlet。
|
||||
要改会话超时或 MIME 类型时才需要改这里。
|
||||
-->
|
||||
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
|
||||
version="6.0"
|
||||
metadata-complete="false">
|
||||
|
||||
<display-name>spring-war17</display-name>
|
||||
|
||||
<session-config>
|
||||
<session-timeout>30</session-timeout>
|
||||
</session-config>
|
||||
|
||||
</web-app>
|
||||
Reference in New Issue
Block a user