- 75
- 0
在springBoot中整合i18n国际化校验时一直出现乱码,具体如下,不知该如何解决呢:
application.properties中配置的有:
spring.messages.basename=i18n/user/message
在resources下有i18n.user包
里面有i18n配置国际化的文件:
message.properties
user.content.name-can-not-be-empty=内容对象名称不能为空
message_zh_CN.properties
user.content.name-can-not-be-empty=内容对象名称不能为空
message_en_US.properties
user.content.name-can-not-be-empty=content object's name can not be empty
现在在entity中添加校验注解,如
@Entity
@Table(name = "user")
@Inheritance(strategy = InheritanceType.JOINED)
@DynamicInsert
public class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull(message = "{user.content.name-can-not-be-empty}")
private String name;
}
想control中传递时自动校验。
根据SpringBoot validator国际化随笔用过如下方法:
@Configuration
public class ValidatorConfiguration {
public ResourceBundleMessageSource getMessageSource() throws Exception {
ResourceBundleMessageSource rbms = new ResourceBundleMessageSource();
rbms.setDefaultEncoding("UTF-8");
rbms.setBasenames("i18n/user/message");
return rbms;
}
@Bean
public Validator getValidator() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(getMessageSource());
return validator;
}
}
但出来的一直是乱码格式:
"message": "Validation failed for classes [com.entity.User] during persist time for groups [javax.validation.groups.Default, ]\nList of constraint violations:[\n\tConstraintViolationImpl{interpolatedMessage='$å\u0086\u0085容对象å\u0090\u008dç§°ä¸\u008dè\u0083½ä¸ºç©º', propertyPath=name, rootBeanClass=class com.entity.User, messageTemplate='${user.content.name-can-not-be-empty}'}\n]",
发现,乱码是使用resources下ValidationMessages.properties系列文件是产生的。
当使用i18n.user包中的文件时,若不在controller中的方法中添加 @Valid或@Validated的注解,则会报
"message": "Validation failed for classes [com.entity.User] during persist time for groups [javax.validation.groups.Default, ]\nList of constraint violations:[\n\tConstraintViolationImpl{interpolatedMessage='{user.content.name-can-not-be-empty}', propertyPath=name, rootBeanClass=class com.entity.User, messageTemplate='{user.content.name-can-not-be-empty}'}\n]",
存在注解如:
@PutMapping(value = "")
public User create( @RequestBody() @Valid User user) {
user= userService.save(user);
return user;
}
返回如下结果:
{
"timestamp": 1516010929111,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"errors": [
{
"codes": [
"NotNull.user.name",
"NotNull.name",
"NotNull.java.lang.String",
"NotNull"
],
"arguments": [
{
"codes": [
"user.name",
"name"
],
"arguments": null,
"defaultMessage": "name",
"code": "name"
}
],
"defaultMessage": "内容对象名称不能为空",
"objectName": "user",
"field": "name",
"rejectedValue": null,
"bindingFailure": false,
"code": "NotNull"
}
],
"message": "Validation failed for object='user'. Error count: 1",
"path": "/api/user"
}
有没有办法,让controller中的方法中不添加 @Valid或@Validated的注解时也能正确返回错误信息呢
- 共 0 条
- 全部回答
-
た靑木知湫ッ 普通会员 1楼
在SpringBoot中,如果你使用了Hibernate的国际化校验功能,但仍然出现乱码,那么可能的原因有以下几点:
- 缺少国际化文件:如果你没有为你的国际化文件添加合适的配置,那么Hibernate可能无法正确地识别这些文件。你可以在你的application.properties或application.yml文件中添加以下内容:
hibernate.i18n.messagesDirectory=/path/to/i18n/messages- 缺少翻译库:如果你没有添加翻译库,那么Hibernate可能无法正确地处理你的国际化字符串。你可以在你的pom.xml文件中添加以下内容:
xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-i18n</artifactId> </dependency>-
缺少国际化字符串:如果你没有为你的国际化字符串添加合适的值,那么Hibernate可能无法正确地处理这些字符串。你可以在你的实体类中添加一个国际化字符串属性,并在你的国际化文件中添加一个对应的翻译。
-
缺少国际化转换器:如果你没有为你的国际化转换器添加合适的配置,那么Hibernate可能无法正确地处理你的国际化字符串。你可以在你的国际化文件中添加一个国际化转换器类,并在你的国际化字符串属性中添加一个对应的国际化转换器。
希望这些信息对你有所帮助!如果你仍然无法解决问题,那么你可能需要进一步的排查和调试。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
