duqing2026 commited on
Commit
3f6ed60
·
1 Parent(s): 68a5fd8

Add Spring Boot Todo API demo

Browse files
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ target/
2
+ .idea/
3
+ .classpath
4
+ .project
5
+ .settings/
6
+ .DS_Store
.mvn/wrapper/maven-wrapper.properties ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ wrapperVersion=3.3.4
2
+ distributionType=only-script
3
+ distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.12/apache-maven-3.9.12-bin.zip
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM maven:3.9.9-eclipse-temurin-21 AS build
2
+ WORKDIR /app
3
+ COPY pom.xml .
4
+ COPY src ./src
5
+ RUN mvn -q -DskipTests package
6
+
7
+ FROM eclipse-temurin:21-jre
8
+ WORKDIR /app
9
+ ENV PORT=7860
10
+ COPY --from=build /app/target/java-demo-0.1.0.jar /app/app.jar
11
+ EXPOSE 7860
12
+ CMD ["java","-jar","/app/app.jar"]
README.md CHANGED
@@ -1,10 +1,81 @@
1
  ---
2
- title: Java Demo
3
- emoji: 🏃
4
- colorFrom: pink
5
- colorTo: yellow
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Java 面试 Demo(Todo API)
3
+ emoji: 🚀
4
+ colorFrom: blue
5
+ colorTo: purple
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
+ # Java 面试 Demo(Todo API)
11
+
12
+ 这是一个用于面试展示的 Java 小项目:基于 Spring Boot 实现一个最小可用的 Todo REST API,并包含参数校验、统一错误返回和基础测试。适配 HuggingFace Spaces(Docker)运行。
13
+
14
+ ## 功能点
15
+
16
+ - Todo CRUD:创建、查询列表、按 id 查询、更新、删除
17
+ - 参数校验:title 必填、长度限制等
18
+ - 统一错误返回:校验错误与 404 统一结构化返回
19
+ - 基础测试:MockMvc 覆盖创建与校验失败场景
20
+ - HuggingFace Spaces:Docker 方式部署,默认监听 7860 端口
21
+
22
+ ## 本地运行
23
+
24
+ 前置:
25
+
26
+ - JDK 21(推荐)
27
+
28
+ 启动:
29
+
30
+ ```bash
31
+ export JAVA_HOME=$(/usr/libexec/java_home -v 21)
32
+ ./mvnw test
33
+ ./mvnw spring-boot:run
34
+ ```
35
+
36
+ 访问:
37
+
38
+ - 首页:http://localhost:7860/
39
+ - 健康检查:http://localhost:7860/api/health
40
+
41
+ ## API 说明
42
+
43
+ ### 创建 Todo
44
+
45
+ ```bash
46
+ curl -s -X POST http://localhost:7860/api/todos \
47
+ -H 'Content-Type: application/json' \
48
+ -d '{"title":"准备面试","description":"把这个项目讲清楚"}'
49
+ ```
50
+
51
+ ### 查询列表
52
+
53
+ ```bash
54
+ curl -s http://localhost:7860/api/todos
55
+ curl -s 'http://localhost:7860/api/todos?status=DONE'
56
+ ```
57
+
58
+ ### 更新 Todo
59
+
60
+ ```bash
61
+ curl -s -X PUT http://localhost:7860/api/todos/1 \
62
+ -H 'Content-Type: application/json' \
63
+ -d '{"title":"准备面试","description":"更新一下状态","status":"DONE"}'
64
+ ```
65
+
66
+ ### 删除 Todo
67
+
68
+ ```bash
69
+ curl -i -X DELETE http://localhost:7860/api/todos/1
70
+ ```
71
+
72
+ ## 设计要点(面试可讲)
73
+
74
+ - 分层:Controller / Service 分离,Service 内存存储(ConcurrentHashMap)
75
+ - 数据结构:使用 record 表达 DTO 与返回值
76
+ - 异常处理:全局异常处理,输出稳定的 ErrorResponse 结构
77
+ - 可部署性:读取 PORT 环境变量并监听 0.0.0.0,适配容器平台
78
+
79
+ ## 部署到 HuggingFace Spaces(Docker)
80
+
81
+ 本仓库已经包含 Dockerfile。将代码推送到 Space 仓库后,Space 会自动构建并启动。
mvnw ADDED
@@ -0,0 +1,295 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ # ----------------------------------------------------------------------------
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ # ----------------------------------------------------------------------------
20
+
21
+ # ----------------------------------------------------------------------------
22
+ # Apache Maven Wrapper startup batch script, version 3.3.4
23
+ #
24
+ # Optional ENV vars
25
+ # -----------------
26
+ # JAVA_HOME - location of a JDK home dir, required when download maven via java source
27
+ # MVNW_REPOURL - repo url base for downloading maven distribution
28
+ # MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
29
+ # MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output
30
+ # ----------------------------------------------------------------------------
31
+
32
+ set -euf
33
+ [ "${MVNW_VERBOSE-}" != debug ] || set -x
34
+
35
+ # OS specific support.
36
+ native_path() { printf %s\\n "$1"; }
37
+ case "$(uname)" in
38
+ CYGWIN* | MINGW*)
39
+ [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")"
40
+ native_path() { cygpath --path --windows "$1"; }
41
+ ;;
42
+ esac
43
+
44
+ # set JAVACMD and JAVACCMD
45
+ set_java_home() {
46
+ # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched
47
+ if [ -n "${JAVA_HOME-}" ]; then
48
+ if [ -x "$JAVA_HOME/jre/sh/java" ]; then
49
+ # IBM's JDK on AIX uses strange locations for the executables
50
+ JAVACMD="$JAVA_HOME/jre/sh/java"
51
+ JAVACCMD="$JAVA_HOME/jre/sh/javac"
52
+ else
53
+ JAVACMD="$JAVA_HOME/bin/java"
54
+ JAVACCMD="$JAVA_HOME/bin/javac"
55
+
56
+ if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then
57
+ echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2
58
+ echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2
59
+ return 1
60
+ fi
61
+ fi
62
+ else
63
+ JAVACMD="$(
64
+ 'set' +e
65
+ 'unset' -f command 2>/dev/null
66
+ 'command' -v java
67
+ )" || :
68
+ JAVACCMD="$(
69
+ 'set' +e
70
+ 'unset' -f command 2>/dev/null
71
+ 'command' -v javac
72
+ )" || :
73
+
74
+ if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then
75
+ echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2
76
+ return 1
77
+ fi
78
+ fi
79
+ }
80
+
81
+ # hash string like Java String::hashCode
82
+ hash_string() {
83
+ str="${1:-}" h=0
84
+ while [ -n "$str" ]; do
85
+ char="${str%"${str#?}"}"
86
+ h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296))
87
+ str="${str#?}"
88
+ done
89
+ printf %x\\n $h
90
+ }
91
+
92
+ verbose() { :; }
93
+ [ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; }
94
+
95
+ die() {
96
+ printf %s\\n "$1" >&2
97
+ exit 1
98
+ }
99
+
100
+ trim() {
101
+ # MWRAPPER-139:
102
+ # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds.
103
+ # Needed for removing poorly interpreted newline sequences when running in more
104
+ # exotic environments such as mingw bash on Windows.
105
+ printf "%s" "${1}" | tr -d '[:space:]'
106
+ }
107
+
108
+ scriptDir="$(dirname "$0")"
109
+ scriptName="$(basename "$0")"
110
+
111
+ # parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties
112
+ while IFS="=" read -r key value; do
113
+ case "${key-}" in
114
+ distributionUrl) distributionUrl=$(trim "${value-}") ;;
115
+ distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;;
116
+ esac
117
+ done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties"
118
+ [ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
119
+
120
+ case "${distributionUrl##*/}" in
121
+ maven-mvnd-*bin.*)
122
+ MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/
123
+ case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in
124
+ *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;;
125
+ :Darwin*x86_64) distributionPlatform=darwin-amd64 ;;
126
+ :Darwin*arm64) distributionPlatform=darwin-aarch64 ;;
127
+ :Linux*x86_64*) distributionPlatform=linux-amd64 ;;
128
+ *)
129
+ echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2
130
+ distributionPlatform=linux-amd64
131
+ ;;
132
+ esac
133
+ distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"
134
+ ;;
135
+ maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;
136
+ *) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
137
+ esac
138
+
139
+ # apply MVNW_REPOURL and calculate MAVEN_HOME
140
+ # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
141
+ [ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}"
142
+ distributionUrlName="${distributionUrl##*/}"
143
+ distributionUrlNameMain="${distributionUrlName%.*}"
144
+ distributionUrlNameMain="${distributionUrlNameMain%-bin}"
145
+ MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}"
146
+ MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")"
147
+
148
+ exec_maven() {
149
+ unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || :
150
+ exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD"
151
+ }
152
+
153
+ if [ -d "$MAVEN_HOME" ]; then
154
+ verbose "found existing MAVEN_HOME at $MAVEN_HOME"
155
+ exec_maven "$@"
156
+ fi
157
+
158
+ case "${distributionUrl-}" in
159
+ *?-bin.zip | *?maven-mvnd-?*-?*.zip) ;;
160
+ *) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;;
161
+ esac
162
+
163
+ # prepare tmp dir
164
+ if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then
165
+ clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; }
166
+ trap clean HUP INT TERM EXIT
167
+ else
168
+ die "cannot create temp dir"
169
+ fi
170
+
171
+ mkdir -p -- "${MAVEN_HOME%/*}"
172
+
173
+ # Download and Install Apache Maven
174
+ verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
175
+ verbose "Downloading from: $distributionUrl"
176
+ verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
177
+
178
+ # select .zip or .tar.gz
179
+ if ! command -v unzip >/dev/null; then
180
+ distributionUrl="${distributionUrl%.zip}.tar.gz"
181
+ distributionUrlName="${distributionUrl##*/}"
182
+ fi
183
+
184
+ # verbose opt
185
+ __MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR=''
186
+ [ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v
187
+
188
+ # normalize http auth
189
+ case "${MVNW_PASSWORD:+has-password}" in
190
+ '') MVNW_USERNAME='' MVNW_PASSWORD='' ;;
191
+ has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;;
192
+ esac
193
+
194
+ if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then
195
+ verbose "Found wget ... using wget"
196
+ wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl"
197
+ elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then
198
+ verbose "Found curl ... using curl"
199
+ curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl"
200
+ elif set_java_home; then
201
+ verbose "Falling back to use Java to download"
202
+ javaSource="$TMP_DOWNLOAD_DIR/Downloader.java"
203
+ targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName"
204
+ cat >"$javaSource" <<-END
205
+ public class Downloader extends java.net.Authenticator
206
+ {
207
+ protected java.net.PasswordAuthentication getPasswordAuthentication()
208
+ {
209
+ return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() );
210
+ }
211
+ public static void main( String[] args ) throws Exception
212
+ {
213
+ setDefault( new Downloader() );
214
+ java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() );
215
+ }
216
+ }
217
+ END
218
+ # For Cygwin/MinGW, switch paths to Windows format before running javac and java
219
+ verbose " - Compiling Downloader.java ..."
220
+ "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java"
221
+ verbose " - Running Downloader.java ..."
222
+ "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")"
223
+ fi
224
+
225
+ # If specified, validate the SHA-256 sum of the Maven distribution zip file
226
+ if [ -n "${distributionSha256Sum-}" ]; then
227
+ distributionSha256Result=false
228
+ if [ "$MVN_CMD" = mvnd.sh ]; then
229
+ echo "Checksum validation is not supported for maven-mvnd." >&2
230
+ echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
231
+ exit 1
232
+ elif command -v sha256sum >/dev/null; then
233
+ if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c - >/dev/null 2>&1; then
234
+ distributionSha256Result=true
235
+ fi
236
+ elif command -v shasum >/dev/null; then
237
+ if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then
238
+ distributionSha256Result=true
239
+ fi
240
+ else
241
+ echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2
242
+ echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
243
+ exit 1
244
+ fi
245
+ if [ $distributionSha256Result = false ]; then
246
+ echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2
247
+ echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2
248
+ exit 1
249
+ fi
250
+ fi
251
+
252
+ # unzip and move
253
+ if command -v unzip >/dev/null; then
254
+ unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip"
255
+ else
256
+ tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar"
257
+ fi
258
+
259
+ # Find the actual extracted directory name (handles snapshots where filename != directory name)
260
+ actualDistributionDir=""
261
+
262
+ # First try the expected directory name (for regular distributions)
263
+ if [ -d "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" ]; then
264
+ if [ -f "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/bin/$MVN_CMD" ]; then
265
+ actualDistributionDir="$distributionUrlNameMain"
266
+ fi
267
+ fi
268
+
269
+ # If not found, search for any directory with the Maven executable (for snapshots)
270
+ if [ -z "$actualDistributionDir" ]; then
271
+ # enable globbing to iterate over items
272
+ set +f
273
+ for dir in "$TMP_DOWNLOAD_DIR"/*; do
274
+ if [ -d "$dir" ]; then
275
+ if [ -f "$dir/bin/$MVN_CMD" ]; then
276
+ actualDistributionDir="$(basename "$dir")"
277
+ break
278
+ fi
279
+ fi
280
+ done
281
+ set -f
282
+ fi
283
+
284
+ if [ -z "$actualDistributionDir" ]; then
285
+ verbose "Contents of $TMP_DOWNLOAD_DIR:"
286
+ verbose "$(ls -la "$TMP_DOWNLOAD_DIR")"
287
+ die "Could not find Maven distribution directory in extracted archive"
288
+ fi
289
+
290
+ verbose "Found extracted Maven distribution directory: $actualDistributionDir"
291
+ printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$actualDistributionDir/mvnw.url"
292
+ mv -- "$TMP_DOWNLOAD_DIR/$actualDistributionDir" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"
293
+
294
+ clean || :
295
+ exec_maven "$@"
mvnw.cmd ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <# : batch portion
2
+ @REM ----------------------------------------------------------------------------
3
+ @REM Licensed to the Apache Software Foundation (ASF) under one
4
+ @REM or more contributor license agreements. See the NOTICE file
5
+ @REM distributed with this work for additional information
6
+ @REM regarding copyright ownership. The ASF licenses this file
7
+ @REM to you under the Apache License, Version 2.0 (the
8
+ @REM "License"); you may not use this file except in compliance
9
+ @REM with the License. You may obtain a copy of the License at
10
+ @REM
11
+ @REM http://www.apache.org/licenses/LICENSE-2.0
12
+ @REM
13
+ @REM Unless required by applicable law or agreed to in writing,
14
+ @REM software distributed under the License is distributed on an
15
+ @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ @REM KIND, either express or implied. See the License for the
17
+ @REM specific language governing permissions and limitations
18
+ @REM under the License.
19
+ @REM ----------------------------------------------------------------------------
20
+
21
+ @REM ----------------------------------------------------------------------------
22
+ @REM Apache Maven Wrapper startup batch script, version 3.3.4
23
+ @REM
24
+ @REM Optional ENV vars
25
+ @REM MVNW_REPOURL - repo url base for downloading maven distribution
26
+ @REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
27
+ @REM MVNW_VERBOSE - true: enable verbose log; others: silence the output
28
+ @REM ----------------------------------------------------------------------------
29
+
30
+ @IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0)
31
+ @SET __MVNW_CMD__=
32
+ @SET __MVNW_ERROR__=
33
+ @SET __MVNW_PSMODULEP_SAVE=%PSModulePath%
34
+ @SET PSModulePath=
35
+ @FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @(
36
+ IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B)
37
+ )
38
+ @SET PSModulePath=%__MVNW_PSMODULEP_SAVE%
39
+ @SET __MVNW_PSMODULEP_SAVE=
40
+ @SET __MVNW_ARG0_NAME__=
41
+ @SET MVNW_USERNAME=
42
+ @SET MVNW_PASSWORD=
43
+ @IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*)
44
+ @echo Cannot start maven from wrapper >&2 && exit /b 1
45
+ @GOTO :EOF
46
+ : end batch / begin powershell #>
47
+
48
+ $ErrorActionPreference = "Stop"
49
+ if ($env:MVNW_VERBOSE -eq "true") {
50
+ $VerbosePreference = "Continue"
51
+ }
52
+
53
+ # calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties
54
+ $distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl
55
+ if (!$distributionUrl) {
56
+ Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
57
+ }
58
+
59
+ switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) {
60
+ "maven-mvnd-*" {
61
+ $USE_MVND = $true
62
+ $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip"
63
+ $MVN_CMD = "mvnd.cmd"
64
+ break
65
+ }
66
+ default {
67
+ $USE_MVND = $false
68
+ $MVN_CMD = $script -replace '^mvnw','mvn'
69
+ break
70
+ }
71
+ }
72
+
73
+ # apply MVNW_REPOURL and calculate MAVEN_HOME
74
+ # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
75
+ if ($env:MVNW_REPOURL) {
76
+ $MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" }
77
+ $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')"
78
+ }
79
+ $distributionUrlName = $distributionUrl -replace '^.*/',''
80
+ $distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$',''
81
+
82
+ $MAVEN_M2_PATH = "$HOME/.m2"
83
+ if ($env:MAVEN_USER_HOME) {
84
+ $MAVEN_M2_PATH = "$env:MAVEN_USER_HOME"
85
+ }
86
+
87
+ if (-not (Test-Path -Path $MAVEN_M2_PATH)) {
88
+ New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null
89
+ }
90
+
91
+ $MAVEN_WRAPPER_DISTS = $null
92
+ if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) {
93
+ $MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists"
94
+ } else {
95
+ $MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists"
96
+ }
97
+
98
+ $MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain"
99
+ $MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
100
+ $MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME"
101
+
102
+ if (Test-Path -Path "$MAVEN_HOME" -PathType Container) {
103
+ Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME"
104
+ Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
105
+ exit $?
106
+ }
107
+
108
+ if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) {
109
+ Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl"
110
+ }
111
+
112
+ # prepare tmp dir
113
+ $TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile
114
+ $TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir"
115
+ $TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null
116
+ trap {
117
+ if ($TMP_DOWNLOAD_DIR.Exists) {
118
+ try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
119
+ catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
120
+ }
121
+ }
122
+
123
+ New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null
124
+
125
+ # Download and Install Apache Maven
126
+ Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
127
+ Write-Verbose "Downloading from: $distributionUrl"
128
+ Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
129
+
130
+ $webclient = New-Object System.Net.WebClient
131
+ if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) {
132
+ $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD)
133
+ }
134
+ [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
135
+ $webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null
136
+
137
+ # If specified, validate the SHA-256 sum of the Maven distribution zip file
138
+ $distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum
139
+ if ($distributionSha256Sum) {
140
+ if ($USE_MVND) {
141
+ Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties."
142
+ }
143
+ Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash
144
+ if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) {
145
+ Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property."
146
+ }
147
+ }
148
+
149
+ # unzip and move
150
+ Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null
151
+
152
+ # Find the actual extracted directory name (handles snapshots where filename != directory name)
153
+ $actualDistributionDir = ""
154
+
155
+ # First try the expected directory name (for regular distributions)
156
+ $expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain"
157
+ $expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD"
158
+ if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) {
159
+ $actualDistributionDir = $distributionUrlNameMain
160
+ }
161
+
162
+ # If not found, search for any directory with the Maven executable (for snapshots)
163
+ if (!$actualDistributionDir) {
164
+ Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object {
165
+ $testPath = Join-Path $_.FullName "bin/$MVN_CMD"
166
+ if (Test-Path -Path $testPath -PathType Leaf) {
167
+ $actualDistributionDir = $_.Name
168
+ }
169
+ }
170
+ }
171
+
172
+ if (!$actualDistributionDir) {
173
+ Write-Error "Could not find Maven distribution directory in extracted archive"
174
+ }
175
+
176
+ Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir"
177
+ Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null
178
+ try {
179
+ Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null
180
+ } catch {
181
+ if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) {
182
+ Write-Error "fail to move MAVEN_HOME"
183
+ }
184
+ } finally {
185
+ try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
186
+ catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
187
+ }
188
+
189
+ Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
pom.xml ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4
+ <modelVersion>4.0.0</modelVersion>
5
+
6
+ <parent>
7
+ <groupId>org.springframework.boot</groupId>
8
+ <artifactId>spring-boot-starter-parent</artifactId>
9
+ <version>3.4.1</version>
10
+ <relativePath/>
11
+ </parent>
12
+
13
+ <groupId>com.duqing2026</groupId>
14
+ <artifactId>java-demo</artifactId>
15
+ <version>0.1.0</version>
16
+ <name>java-demo</name>
17
+ <description>Interview-ready Java demo project</description>
18
+
19
+ <properties>
20
+ <java.version>21</java.version>
21
+ </properties>
22
+
23
+ <dependencies>
24
+ <dependency>
25
+ <groupId>org.springframework.boot</groupId>
26
+ <artifactId>spring-boot-starter-web</artifactId>
27
+ </dependency>
28
+ <dependency>
29
+ <groupId>org.springframework.boot</groupId>
30
+ <artifactId>spring-boot-starter-validation</artifactId>
31
+ </dependency>
32
+
33
+ <dependency>
34
+ <groupId>org.springframework.boot</groupId>
35
+ <artifactId>spring-boot-starter-test</artifactId>
36
+ <scope>test</scope>
37
+ </dependency>
38
+ </dependencies>
39
+
40
+ <build>
41
+ <plugins>
42
+ <plugin>
43
+ <groupId>org.springframework.boot</groupId>
44
+ <artifactId>spring-boot-maven-plugin</artifactId>
45
+ </plugin>
46
+ <plugin>
47
+ <groupId>org.apache.maven.plugins</groupId>
48
+ <artifactId>maven-surefire-plugin</artifactId>
49
+ <configuration>
50
+ <argLine>-XX:+EnableDynamicAgentLoading</argLine>
51
+ </configuration>
52
+ </plugin>
53
+ </plugins>
54
+ </build>
55
+ </project>
src/main/java/com/duqing2026/javademo/JavaDemoApplication.java ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo;
2
+
3
+ import org.springframework.boot.SpringApplication;
4
+ import org.springframework.boot.autoconfigure.SpringBootApplication;
5
+
6
+ @SpringBootApplication
7
+ public class JavaDemoApplication {
8
+ public static void main(String[] args) {
9
+ SpringApplication.run(JavaDemoApplication.class, args);
10
+ }
11
+ }
src/main/java/com/duqing2026/javademo/api/ErrorResponse.java ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.api;
2
+
3
+ import java.time.Instant;
4
+ import java.util.Map;
5
+
6
+ public record ErrorResponse(
7
+ Instant timestamp,
8
+ int status,
9
+ String error,
10
+ String message,
11
+ String path,
12
+ Map<String, Object> details
13
+ ) {
14
+ }
src/main/java/com/duqing2026/javademo/api/GlobalExceptionHandler.java ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.api;
2
+
3
+ import jakarta.servlet.http.HttpServletRequest;
4
+ import org.springframework.http.HttpStatus;
5
+ import org.springframework.http.ResponseEntity;
6
+ import org.springframework.validation.FieldError;
7
+ import org.springframework.web.bind.MethodArgumentNotValidException;
8
+ import org.springframework.web.bind.annotation.ExceptionHandler;
9
+ import org.springframework.web.bind.annotation.RestControllerAdvice;
10
+
11
+ import java.time.Instant;
12
+ import java.util.LinkedHashMap;
13
+ import java.util.Map;
14
+
15
+ @RestControllerAdvice
16
+ public class GlobalExceptionHandler {
17
+
18
+ @ExceptionHandler(MethodArgumentNotValidException.class)
19
+ public ResponseEntity<ErrorResponse> handleValidation(MethodArgumentNotValidException ex, HttpServletRequest request) {
20
+ Map<String, Object> details = new LinkedHashMap<>();
21
+ for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
22
+ details.put(fieldError.getField(), fieldError.getDefaultMessage());
23
+ }
24
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(
25
+ new ErrorResponse(
26
+ Instant.now(),
27
+ HttpStatus.BAD_REQUEST.value(),
28
+ HttpStatus.BAD_REQUEST.getReasonPhrase(),
29
+ "参数校验失败",
30
+ request.getRequestURI(),
31
+ details
32
+ )
33
+ );
34
+ }
35
+
36
+ @ExceptionHandler(TodoNotFoundException.class)
37
+ public ResponseEntity<ErrorResponse> handleNotFound(TodoNotFoundException ex, HttpServletRequest request) {
38
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body(
39
+ new ErrorResponse(
40
+ Instant.now(),
41
+ HttpStatus.NOT_FOUND.value(),
42
+ HttpStatus.NOT_FOUND.getReasonPhrase(),
43
+ ex.getMessage(),
44
+ request.getRequestURI(),
45
+ Map.of()
46
+ )
47
+ );
48
+ }
49
+
50
+ @ExceptionHandler(Exception.class)
51
+ public ResponseEntity<ErrorResponse> handleOther(Exception ex, HttpServletRequest request) {
52
+ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
53
+ new ErrorResponse(
54
+ Instant.now(),
55
+ HttpStatus.INTERNAL_SERVER_ERROR.value(),
56
+ HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
57
+ "服务端异常",
58
+ request.getRequestURI(),
59
+ Map.of("exception", ex.getClass().getName())
60
+ )
61
+ );
62
+ }
63
+ }
src/main/java/com/duqing2026/javademo/api/TodoNotFoundException.java ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.api;
2
+
3
+ public class TodoNotFoundException extends RuntimeException {
4
+ public TodoNotFoundException(long id) {
5
+ super("Todo 不存在: id=" + id);
6
+ }
7
+ }
src/main/java/com/duqing2026/javademo/system/HealthController.java ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.system;
2
+
3
+ import org.springframework.web.bind.annotation.GetMapping;
4
+ import org.springframework.web.bind.annotation.RestController;
5
+
6
+ import java.time.Instant;
7
+ import java.util.Map;
8
+
9
+ @RestController
10
+ public class HealthController {
11
+ @GetMapping("/api/health")
12
+ public Map<String, Object> health() {
13
+ return Map.of(
14
+ "status", "UP",
15
+ "time", Instant.now().toString()
16
+ );
17
+ }
18
+ }
src/main/java/com/duqing2026/javademo/todo/TodoController.java ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.todo;
2
+
3
+ import jakarta.validation.Valid;
4
+ import org.springframework.http.HttpStatus;
5
+ import org.springframework.web.bind.annotation.*;
6
+
7
+ import java.util.List;
8
+ import java.util.Optional;
9
+
10
+ @RestController
11
+ @RequestMapping("/api/todos")
12
+ public class TodoController {
13
+ private final TodoService todoService;
14
+
15
+ public TodoController(TodoService todoService) {
16
+ this.todoService = todoService;
17
+ }
18
+
19
+ @PostMapping
20
+ @ResponseStatus(HttpStatus.CREATED)
21
+ public TodoItem create(@Valid @RequestBody TodoCreateRequest request) {
22
+ return todoService.create(request);
23
+ }
24
+
25
+ @GetMapping
26
+ public List<TodoItem> list(@RequestParam(required = false) TodoStatus status) {
27
+ return todoService.list(Optional.ofNullable(status));
28
+ }
29
+
30
+ @GetMapping("/{id}")
31
+ public TodoItem get(@PathVariable long id) {
32
+ return todoService.get(id);
33
+ }
34
+
35
+ @PutMapping("/{id}")
36
+ public TodoItem update(@PathVariable long id, @Valid @RequestBody TodoUpdateRequest request) {
37
+ return todoService.update(id, request);
38
+ }
39
+
40
+ @DeleteMapping("/{id}")
41
+ @ResponseStatus(HttpStatus.NO_CONTENT)
42
+ public void delete(@PathVariable long id) {
43
+ todoService.delete(id);
44
+ }
45
+ }
src/main/java/com/duqing2026/javademo/todo/TodoCreateRequest.java ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.todo;
2
+
3
+ import jakarta.validation.constraints.NotBlank;
4
+ import jakarta.validation.constraints.Size;
5
+
6
+ public record TodoCreateRequest(
7
+ @NotBlank(message = "title 不能为空")
8
+ @Size(max = 80, message = "title 最多 80 个字符")
9
+ String title,
10
+
11
+ @Size(max = 400, message = "description 最多 400 个字符")
12
+ String description
13
+ ) {
14
+ }
src/main/java/com/duqing2026/javademo/todo/TodoItem.java ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.todo;
2
+
3
+ import java.time.Instant;
4
+
5
+ public record TodoItem(
6
+ long id,
7
+ String title,
8
+ String description,
9
+ TodoStatus status,
10
+ Instant createdAt,
11
+ Instant updatedAt
12
+ ) {
13
+ }
src/main/java/com/duqing2026/javademo/todo/TodoService.java ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.todo;
2
+
3
+ import com.duqing2026.javademo.api.TodoNotFoundException;
4
+ import org.springframework.stereotype.Service;
5
+
6
+ import java.time.Instant;
7
+ import java.util.Comparator;
8
+ import java.util.List;
9
+ import java.util.Optional;
10
+ import java.util.concurrent.ConcurrentHashMap;
11
+ import java.util.concurrent.ConcurrentMap;
12
+ import java.util.concurrent.atomic.AtomicLong;
13
+
14
+ @Service
15
+ public class TodoService {
16
+ private final AtomicLong idGenerator = new AtomicLong(0);
17
+ private final ConcurrentMap<Long, TodoItem> store = new ConcurrentHashMap<>();
18
+
19
+ public TodoItem create(TodoCreateRequest request) {
20
+ long id = idGenerator.incrementAndGet();
21
+ Instant now = Instant.now();
22
+ TodoItem item = new TodoItem(
23
+ id,
24
+ request.title().trim(),
25
+ normalizeDescription(request.description()),
26
+ TodoStatus.OPEN,
27
+ now,
28
+ now
29
+ );
30
+ store.put(id, item);
31
+ return item;
32
+ }
33
+
34
+ public List<TodoItem> list(Optional<TodoStatus> status) {
35
+ return store.values().stream()
36
+ .filter(item -> status.map(s -> item.status() == s).orElse(true))
37
+ .sorted(Comparator.comparingLong(TodoItem::id))
38
+ .toList();
39
+ }
40
+
41
+ public TodoItem get(long id) {
42
+ TodoItem item = store.get(id);
43
+ if (item == null) {
44
+ throw new TodoNotFoundException(id);
45
+ }
46
+ return item;
47
+ }
48
+
49
+ public TodoItem update(long id, TodoUpdateRequest request) {
50
+ return store.compute(id, (key, existing) -> {
51
+ if (existing == null) {
52
+ throw new TodoNotFoundException(id);
53
+ }
54
+ Instant now = Instant.now();
55
+ TodoStatus nextStatus = request.status() == null ? existing.status() : request.status();
56
+ return new TodoItem(
57
+ existing.id(),
58
+ request.title().trim(),
59
+ normalizeDescription(request.description()),
60
+ nextStatus,
61
+ existing.createdAt(),
62
+ now
63
+ );
64
+ });
65
+ }
66
+
67
+ public void delete(long id) {
68
+ TodoItem removed = store.remove(id);
69
+ if (removed == null) {
70
+ throw new TodoNotFoundException(id);
71
+ }
72
+ }
73
+
74
+ private String normalizeDescription(String description) {
75
+ if (description == null) {
76
+ return null;
77
+ }
78
+ String trimmed = description.trim();
79
+ return trimmed.isEmpty() ? null : trimmed;
80
+ }
81
+ }
src/main/java/com/duqing2026/javademo/todo/TodoStatus.java ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.todo;
2
+
3
+ public enum TodoStatus {
4
+ OPEN,
5
+ DONE
6
+ }
src/main/java/com/duqing2026/javademo/todo/TodoUpdateRequest.java ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.todo;
2
+
3
+ import jakarta.validation.constraints.NotBlank;
4
+ import jakarta.validation.constraints.Size;
5
+
6
+ public record TodoUpdateRequest(
7
+ @NotBlank(message = "title 不能为空")
8
+ @Size(max = 80, message = "title 最多 80 个字符")
9
+ String title,
10
+
11
+ @Size(max = 400, message = "description 最多 400 个字符")
12
+ String description,
13
+
14
+ TodoStatus status
15
+ ) {
16
+ }
src/main/resources/application.properties ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ server.address=0.0.0.0
2
+ server.port=${PORT:7860}
3
+ spring.jackson.time-zone=UTC
src/main/resources/static/index.html ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8"/>
5
+ <meta name="viewport" content="width=device-width, initial-scale=1"/>
6
+ <title>Java Demo - Todo API</title>
7
+ <style>
8
+ body { font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial; margin: 32px; line-height: 1.6; }
9
+ code, pre { background: #f6f8fa; padding: 2px 6px; border-radius: 6px; }
10
+ pre { padding: 12px; overflow: auto; }
11
+ .box { border: 1px solid #e5e7eb; border-radius: 10px; padding: 16px; margin: 16px 0; }
12
+ </style>
13
+ </head>
14
+ <body>
15
+ <h1>Java Demo:Todo API</h1>
16
+ <p>这是一个用于面试展示的 Java/Spring Boot 小项目:提供一个最小可用的 Todo 管理 REST API,并包含参数校验、统一错误返回与单元测试。</p>
17
+
18
+ <div class="box">
19
+ <h2>快速检查</h2>
20
+ <p><a href="/api/health">/api/health</a></p>
21
+ </div>
22
+
23
+ <div class="box">
24
+ <h2>API 示例(curl)</h2>
25
+ <pre><code>curl -s http://localhost:7860/api/health
26
+
27
+ curl -s -X POST http://localhost:7860/api/todos \
28
+ -H 'Content-Type: application/json' \
29
+ -d '{"title":"准备面试","description":"把这个项目讲清楚"}'
30
+
31
+ curl -s http://localhost:7860/api/todos
32
+
33
+ curl -s -X PUT http://localhost:7860/api/todos/1 \
34
+ -H 'Content-Type: application/json' \
35
+ -d '{"title":"准备面试","description":"更新一下状态","status":"DONE"}'
36
+
37
+ curl -i -X DELETE http://localhost:7860/api/todos/1</code></pre>
38
+ </div>
39
+
40
+ <p>更多说明见仓库 README.md。</p>
41
+ </body>
42
+ </html>
src/test/java/com/duqing2026/javademo/todo/TodoControllerTest.java ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.duqing2026.javademo.todo;
2
+
3
+ import com.fasterxml.jackson.databind.ObjectMapper;
4
+ import org.junit.jupiter.api.Test;
5
+ import org.springframework.beans.factory.annotation.Autowired;
6
+ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
7
+ import org.springframework.boot.test.context.SpringBootTest;
8
+ import org.springframework.http.MediaType;
9
+ import org.springframework.test.web.servlet.MockMvc;
10
+
11
+ import static org.hamcrest.Matchers.*;
12
+ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
13
+ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
14
+
15
+ @SpringBootTest
16
+ @AutoConfigureMockMvc
17
+ class TodoControllerTest {
18
+
19
+ @Autowired
20
+ private MockMvc mockMvc;
21
+
22
+ @Autowired
23
+ private ObjectMapper objectMapper;
24
+
25
+ @Test
26
+ void createAndGetTodo() throws Exception {
27
+ TodoCreateRequest createRequest = new TodoCreateRequest("准备面试", "写一个可运行的 demo");
28
+
29
+ String createdJson = mockMvc.perform(post("/api/todos")
30
+ .contentType(MediaType.APPLICATION_JSON)
31
+ .content(objectMapper.writeValueAsString(createRequest)))
32
+ .andExpect(status().isCreated())
33
+ .andExpect(jsonPath("$.id", greaterThan(0)))
34
+ .andExpect(jsonPath("$.title", is("准备面试")))
35
+ .andExpect(jsonPath("$.status", is("OPEN")))
36
+ .andReturn()
37
+ .getResponse()
38
+ .getContentAsString();
39
+
40
+ TodoItem created = objectMapper.readValue(createdJson, TodoItem.class);
41
+
42
+ mockMvc.perform(get("/api/todos/{id}", created.id()))
43
+ .andExpect(status().isOk())
44
+ .andExpect(jsonPath("$.id", is((int) created.id())))
45
+ .andExpect(jsonPath("$.title", is("准备面试")));
46
+ }
47
+
48
+ @Test
49
+ void validationErrorReturnsDetails() throws Exception {
50
+ mockMvc.perform(post("/api/todos")
51
+ .contentType(MediaType.APPLICATION_JSON)
52
+ .content("{\"title\":\"\",\"description\":\"x\"}"))
53
+ .andExpect(status().isBadRequest())
54
+ .andExpect(jsonPath("$.message", is("参数校验失败")))
55
+ .andExpect(jsonPath("$.details.title", notNullValue()));
56
+ }
57
+ }