使用spring boot 实现返回信息国际化记录
来源:互联网 发布:js获取日期时间戳 编辑:程序博客网 时间:2024/05/07 21:18
一、内容
本文主要是记录spring boot 使用enum + MessageSource + *.properties 实现国际化问题
二、步骤
2.1 新建一个枚举(enum)
枚举主要是用来存放返回信息的编号和代码,如
注意:这里的错误代码是.properties
文件中对象的key值,描述则是相应的值,分为英文和中文,在messages_zh_CN.properties
文件中为“密码错误”,在messages_en_US.properties
中为“the password is illegal”。
代码如下
/** * @author lcl * @createDate 2016年11月21日上午8:25:17 * 错误返回信息枚举 */public enum ErrorStatus { ACCOUNT_EXISTED(10001), private int code; private ErrorStatus(int code) { this.code = code; } public int getCode() { return code; }}
2.2 配置MessageSource实体
该类主要是从资源文件中根据key和locale获取相应的值
@Configurationpublic class InternationalConfig { @Value(value = "${spring.messages.basename}") private String basename; @Bean(name = "messageSource") public ResourceBundleMessageSource getMessageResource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename(basename); return messageSource; }}
2.3 创建相应的资源文件
2.3.1 创建不同的文件,并根据枚举中的name创建相应的信息
2.3.2 在application.properties中配置相关的message
2.4 使用messagesource获取资源文件中的信息
这一步根据自己的业务需求获取
@Componentpublic class ErrorMessageSourceHandler { @Autowired private MessageSource messageSource; /** * 根据 * @param messageKey * @return */ public String getMessage(String messageKey) { String message = messageSource.getMessage(messageKey, null, LocaleContextHolder.getLocale()); return message; }}
0 0
- 使用spring boot 实现返回信息国际化记录
- Spring boot 实现国际化
- spring boot使用记录
- Spring boot使用logback实现日志的记录
- Spring-boot-devtools使用记录
- 使用Spring MVC 实现 国际化
- 使用spring实现资源国际化
- spring boot 国际化
- Spring Boot国际化支持
- Spring Boot国际化(i18n)
- Spring boot中使用log4j记录日志
- Spring boot中使用log4j记录日志
- 使用 JSONDoc 记录 Spring Boot RESTful API
- Spring boot中使用log4j记录日志
- spring boot 返回 html
- Spring security异常信息国际化
- Spring security异常信息国际化
- spring——国际化信息
- spark coalesce和repartition的区别
- 事件监听器
- Android 权限大全
- 欢迎使用CSDN-markdown编辑器
- strcpy, strncpy, strcat, strncat, sprintf, snprintf等常用函数
- 使用spring boot 实现返回信息国际化记录
- java线程异常的处理
- 解析并触发groovy脚本
- 使用字符串总结
- mongo查询总结-spring-data-mongo
- 最长公共子序列问题(Java)-动态规划
- linux下安装chrome
- 全文展示以及收起展示
- git add .和git add -u和git add -A的区别