Springboot+AOP实现返回数据提示语国际化的示例代码
Springboot+AOP实现返回数据提示语国际化的示例代码
这篇文章主要介绍了Springboot+AOP实现返回数据提示语国际化的示例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
前言
本篇内容:
提示语的国际化返回,自定义多语言。
本文使用aop方式,拦截接口返回的数据,进行转换。
正文
先看这次示例教学的项目 目录结构:
(当然resource里面的i18n文件夹和三个properties文件也是要我们自己建的,但是 那个Resource Bundle 不用管,这个在yml加上对应配置项自动生成的。 不清楚的继续往下看教学就好)
开始敲(CV)代码:
pom.xml 依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version> 1.2 . 68 </version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version> 3.9 </version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 10 </version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> |
返回码的枚举
CodeEnum.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * @author JCccc */ public enum CodeEnum { SUCCESS( 1000 , "请求成功" ), FAIL( 2000 , "请求失败" ); public final int code; public final String msg; public Integer getCode() { return this .code; } CodeEnum( int code, String msg) { this .code = code; this .msg = msg; } public String getMsg() { return this .msg; } } |
返回数据的简单封装
ResultData.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import com.test.myi18n.enums.CodeEnum; import lombok.Data; /** * @author JCccc */ @Data public class ResultData<T> { private Integer code; private String message; private T data; public ResultData( int code, String message) { this .code = code; this .message = message; } public static ResultData success(CodeEnum codeEnum) { return new ResultData(codeEnum.code, codeEnum.msg); } public static ResultData success(String msg) { return new ResultData(CodeEnum.SUCCESS.getCode(),msg); } } |
Locale、 MessageSource的简单方法封装
LocaleMessage.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Component; import java.util.Locale; /** * @author JCccc */ @Component public class LocaleMessage { @Autowired private MessageSource messageSource; public String getMessage(String code){ return this .getMessage(code, new Object[]{}); } public String getMessage(String code,String defaultMessage){ return this .getMessage(code, null ,defaultMessage); } public String getMessage(String code,String defaultMessage,Locale locale){ return this .getMessage(code, null ,defaultMessage,locale); } public String getMessage(String code,Locale locale){ return this .getMessage(code, null , "" ,locale); } public String getMessage(String code,Object[] args){ return this .getMessage(code,args, "" ); } public String getMessage(String code,Object[] args,Locale locale){ return this .getMessage(code,args, "" ,locale); } public String getMessage(String code,Object[] args,String defaultMessage){ return this .getMessage(code,args, defaultMessage,LocaleContextHolder.getLocale()); } public String getMessage(String code,Object[]args,String defaultMessage,Locale locale){ return messageSource.getMessage(code,args, defaultMessage,locale); } } |
i18n语言转换工具类
I18nUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.util.Locale; import com.test.myi18n.message.LocaleMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class I18nUtils { @Autowired private LocaleMessage localeMessage; /** * 获取key * * @param key * @return */ public String getKey(String key) { String name = localeMessage.getMessage(key); return name; } /** * 获取指定哪个配置文件下的key * * @param key * @param local * @return */ public String getKey(String key, Locale local) { String name = localeMessage.getMessage(key, local); return name; } } |
LanguageAspect.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * @author JCccc */ @Aspect @Component @AllArgsConstructor @ConditionalOnProperty (prefix = "lang" , name = "open" , havingValue = "true" ) public class LanguageAspect { @Autowired I18nUtils i18nUtils; @Pointcut ( "execution(* com.test.myi18n.controller.*.*(..)))" ) public void annotationLangCut() { } /** * 拦截controller层返回的结果,修改msg字段 * * @param point * @param obj */ @AfterReturning (pointcut = "annotationLangCut()" , returning = "obj" ) public void around(JoinPoint point, Object obj) { Object resultObject = obj; try { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); //从获取RequestAttributes中获取HttpServletRequest的信息 HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST); String langFlag = request.getHeader( "lang" ); if ( null != langFlag) { ResultData r = (ResultData) obj; String msg = r.getMessage().trim(); if (StringUtils.isNotEmpty(msg)) { if ( "CN" .equals(langFlag)) { Locale locale = Locale.CHINA; msg = i18nUtils.getKey(msg, locale); } else if ( "EN" .equals(langFlag)) { Locale locale = Locale.US; msg = i18nUtils.getKey(msg, locale); } else { msg = i18nUtils.getKey(msg); } } r.setMessage(msg); } } catch (Exception e) { e.printStackTrace(); //返回原值 obj = resultObject; } } } |
代码简单解读:
1. annotationLangCut 上面切点管控的地址 需要自己改下,改成自己想管控的文件夹路径2. @ConditionalOnProperty 注解,读取yml 里面lang开头的配置项,key为 open ,value 为true
只有为true,这个aop拦截才会生效
3. String langFlag = request.getHeader("lang");
从这句可以看到我这次文章采取的是让对接接口方(前端)在header里面传入需要使用的语言flag。 例如传入 EN (英文),意思就是需要把提示语转为英文。
大家可以结合自己的项目实际情况,改为从yml读取或者从数据库读取或者从redis读取等等都可以。4. ResultData r = (ResultData) obj;
String msg = r.getMessage().trim();
这两行代码为了就是把拦截到的obj中的message提示语获取出来, 如果大家项目的返回数据不是我文中使用的 ResultData,则需要自己进行魔改调整。
最后是 三份 mess properties文件:
mess.properties
自定义的返回语= 您好,如果文章对你有用,请关注+收藏+评论
这个文件按照本文里aop的拦截方式,会先检测 当前 的 语言flag值,如果检测不到就h会到
mess.properties 文件里面找。
mess_en_US.properties
请求成功=success
请求失败=fail
mess_zh_CN.properties
请求成功=请求成功
请求失败=请求失败
success=请求成功
fail=请求失败
最后写个测试接口给大家演示一下效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 | @GetMapping ( "test" ) public ResultData test( @RequestParam int testNum) { if ( 1 ==testNum){ return ResultData.success(CodeEnum.SUCCESS); } if ( 2 ==testNum){ return ResultData.success(CodeEnum.FAIL); } if ( 3 ==testNum){ return ResultData.success( "自定义的返回语" ); } return ResultData.success(CodeEnum.SUCCESS); } |
调用测试:
好,就先到这。
到此这篇关于Springboot+AOP实现返回数据提示语国际化的示例代码的文章就介绍到这了,更多相关Springboot 返回数据提示语内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
SpringBoot参数校验与国际化使用教程
SpringBoot消息国际化配置实现过程解析
SpringBoot实现国际化过程详解
Springboot 使用 JSR 303 对 Controller 控制层校验及 Service 服务层 AOP 校验 使用消息资源文件对消息国际化
springboot+thymeleaf国际化之LocaleResolver接口的示例
原文链接:https://blog.csdn.net/qq_35387940/article/details/118992865