X

Java,SpringBoot,Validation验证框架,提示验证消息国际化

前言

国际化信息也称为“本地化信息”,一般需要两个条件才可以确定一个特定类型的本地化信息,它们分别是“语言类型”和“国家/地区的类型”。如中文本地化信息既有中国大陆地区的中文,又有中国台湾、中国香港地区的中文,还有新加坡地区的中文,Java通过java.util.Locale类表示一个本地化对象,它允许通过语言参数和国家/地区参数创建一个确定的本地化对象。

之前内容:Validation验证框架,@Validated和@Valid验证统一返回错误信息自定义Validator验证

代码

接收参数类:

import lombok.Data;import javax.validation.constraints.*;import java.io.Serializable;import java.util.List;@Datapublic class UserParam implements Serializable { private static final long serialVersionUID = 3869883763128105939L; // 名称 @NotEmpty(message = "{vo.user.NAME_NOT_EMPTY}") private String name; // 账号 @NotBlank(message = "{vo.user.ACCOUNT_NOT_EMPTY}") private String account; // 地址 @NotBlank(message = "{vo.user.ADDRESS_NOT_EMPTY}") private String address; // 密码 @Size(min = 6, max = 20, message = "{vo.user.PASSWORD_SIZE}") @NotBlank(message = "{vo.user.PASSWORD_NOT_EMPTY}") private String password; // 邮箱 @Email(message = "{vo.user.EMAIL_ERROR_FORMAT}") @NotBlank(message = "{vo.user.EMAIL_NOT_EMPTY}") private String email; // 年龄 @Min(value = 18, message = "{vo.user.AGE_MUST_GREATERTHAN_18}") private int age; // 车牌号 @NotNull(message = "{vo.user.PLATENO_NOT_EMPTY}") private String plateNo; // 业余爱好 @NotEmpty(message = "{vo.user.HOBILLES_NOT_EMPTY}") private List<String> hobbies;}

控制器:

import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;@Slf4j@Controller@RequestMapping(value = "/user")public class UserController { @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST}) @ResponseBody public Object addUser(@RequestBody @Validated UserParam userParam, BindingResult bindingResult) { log.info("---addUser ---userParam:{}", userParam.toString()); String result = "成功:" + userParam; //如果验证通不过的处理 if (bindingResult.hasErrors()) { //打印错误 result = bindingResult.getFieldError().getDefaultMessage(); } log.info("---addUser ---result:{}", result); return result; }}

资源文件:

ValidationMessages.properties

vo.user.NAME_NOT_EMPTY=姓名不能为空vo.user.AGE_NOT_EMPTY=年龄不能为空vo.user.AGE_MUST_GREATERTHAN_18=年龄必须大于等于18岁vo.user.HOBILLES_NOT_EMPTY=兴趣爱好不能为空vo.user.ACCOUNT_NOT_EMPTY=账号不能为空vo.user.PLATENO_NOT_EMPTY=车牌号码不能为空vo.user.ADDRESS_NOT_EMPTY=地址不能为空vo.user.PASSWORD_SIZE=密码的长度应该在6和20之间vo.user.PASSWORD_NOT_EMPTY=密码不能为空vo.user.EMAIL_ERROR_FORMAT=邮箱格式错误vo.user.EMAIL_NOT_EMPTY=邮箱不能为空

ValidationMessages_en.properties

vo.user.NAME_NOT_EMPTY=The name cannot be emptyvo.user.AGE_NOT_EMPTY=The age cannot be emptyvo.user.AGE_MUST_GREATERTHAN_18=Must be at least 18 years oldvo.user.HOBILLES_NOT_EMPTY=A hobby cannot be emptyvo.user.ACCOUNT_NOT_EMPTY=The account cannot be emptyvo.user.PLATENO_NOT_EMPTY=The license plate number cannot be blankvo.user.ADDRESS_NOT_EMPTY=The address cannot be emptyvo.user.PASSWORD_SIZE=The length of the password should be between 6 and 20vo.user.PASSWORD_NOT_EMPTY=The password cannot be emptyvo.user.EMAIL_ERROR_FORMAT=Mailbox format errorvo.user.EMAIL_NOT_EMPTY=The mailbox cannot be empty

ValidationMessages_zh.properties

vo.user.NAME_NOT_EMPTY=姓名不能为空vo.user.AGE_NOT_EMPTY=年龄不能为空vo.user.AGE_MUST_GREATERTHAN_18=年龄必须大于等于18岁vo.user.HOBILLES_NOT_EMPTY=兴趣爱好不能为空vo.user.ACCOUNT_NOT_EMPTY=账号不能为空vo.user.PLATENO_NOT_EMPTY=车牌号码不能为空vo.user.ADDRESS_NOT_EMPTY=地址不能为空vo.user.PASSWORD_SIZE=密码的长度应该在6和20之间vo.user.PASSWORD_NOT_EMPTY=密码不能为空vo.user.EMAIL_ERROR_FORMAT=邮箱格式错误vo.user.EMAIL_NOT_EMPTY=邮箱不能为空测试

测试地址: http://127.0.0.1:8080/user/add

测试情况: