# springboot-i18n-validation-test **Repository Path**: baOZe/springboot-i18n-validation-test ## Basic Information - **Project Name**: springboot-i18n-validation-test - **Description**: 基于 spring boot 2.3.2,测试 i18n 与 validation 的集成使用,带有单元测试 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-11 - **Last Updated**: 2025-07-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Spring Boot i18n Validation Test 这是一个Spring Boot项目,演示了如何集成国际化(i18n)和数据验证(validation)功能。 ## 项目特性 - **Spring Boot 2.3.2.RELEASE** - **JDK 1.8** - **国际化支持(i18n)** - **数据验证(validation)** - **统一异常处理** - **RESTful API** - **Lombok支持** - 自动生成getter/setter方法 - **重要发现** - 空的messages.properties文件对MessageSource初始化的关键作用 ## 项目结构 ``` src/ ├── main/ │ ├── java/ │ │ └── com/ │ │ └── example/ │ │ ├── Application.java # 主应用程序类 │ │ ├── config/ │ │ │ └── I18nConfig.java # 国际化配置 │ │ ├── controller/ │ │ │ └── UserController.java # 用户控制器 │ │ ├── entity/ │ │ │ └── User.java # 用户实体类 │ │ ├── common/ │ │ │ └── ApiResponse.java # 统一响应类 │ │ └── exception/ │ │ └── GlobalExceptionHandler.java # 全局异常处理器 │ └── resources/ │ ├── application.yml # 应用配置文件 │ └── i18n/ │ └── messages_en_US.properties # 英文国际化资源文件 ``` ## 配置说明 ### application.yml 配置 ```yaml spring: messages: basename: i18n/messages # 国际化资源文件路径 encoding: UTF-8 # 编码格式 cache-duration: 3600 # 缓存时间 default-locale: en_US # 默认语言 fallback-to-system-locale: true # 回退到系统语言 ``` ### 国际化配置 项目使用 `SessionLocaleResolver` 进行语言切换,通过 `lang` 参数切换语言: - 默认语言:`en_US` - 切换语言:`?lang=zh_CN` 或 `?lang=en_US` ## API 接口 ### 1. 创建用户(测试validation) ```http POST /api/users Content-Type: application/json { "name": "John Doe", "email": "john@example.com", "age": 25, "phone": "13800138000" } ``` ### 2. 获取欢迎信息(测试i18n) ```http GET /api/users/welcome GET /api/users/welcome?lang=en_US ``` ### 3. 获取问候信息(测试i18n参数) ```http GET /api/users/hello/World GET /api/users/hello/World?lang=en_US ``` ### 4. 获取当前时间(测试i18n参数) ```http GET /api/users/time GET /api/users/time?lang=en_US ``` ### 5. 获取用户(测试路径参数验证) ```http GET /api/users/1 GET /api/users/0 # 会触发验证错误 ``` ### 6. 搜索用户(测试查询参数验证) ```http GET /api/users/search?keyword=test&page=1 GET /api/users/search?keyword=a&page=0 # 会触发验证错误 ``` ## 验证规则 ### User 实体验证规则 - `name`: 不能为空,长度2-50字符 - `email`: 不能为空,必须是有效邮箱格式 - `age`: 不能为空,范围1-150 - `phone`: 必须是有效的中国手机号格式 ### 控制器参数验证 - 路径参数 `id`: 最小值1 - 查询参数 `keyword`: 长度2-20字符 - 查询参数 `page`: 最小值1 ## 运行项目 1. 确保已安装 JDK 1.8 和 Maven 2. 克隆项目到本地 3. 在项目根目录执行: ```bash mvn spring-boot:run ``` 4. 访问 http://localhost:8080 ## 测试示例 ### 测试验证功能 ```bash # 创建有效用户 curl -X POST http://localhost:8080/api/users \ -H "Content-Type: application/json" \ -d '{ "name": "John Doe", "email": "john@example.com", "age": 25, "phone": "13800138000" }' # 创建无效用户(会触发验证错误) curl -X POST http://localhost:8080/api/users \ -H "Content-Type: application/json" \ -d '{ "name": "", "email": "invalid-email", "age": -1, "phone": "123" }' ``` ### 测试国际化功能 ```bash # 获取欢迎信息(默认英文) curl http://localhost:8080/api/users/welcome # 获取问候信息 curl http://localhost:8080/api/users/hello/World # 获取当前时间 curl http://localhost:8080/api/users/time ``` ## 错误处理 项目使用全局异常处理器统一处理验证错误,所有错误消息都通过国际化资源文件提供,支持多语言显示。 验证失败时会返回如下格式的错误响应: ```json { "success": false, "message": "Invalid request", "errors": [ "User name cannot be empty", "Email format is invalid", "Age must be at least 1" ] } ```