当前位置: 首页 > news >正文

SpringBoot错误码国际化

先看测试效果: 

f7d7139e9f424832bcfdc5ee3863e0cb.png

130cff648d04407f8596fc3fa198e4ee.png

 374f7511d19047e999d8f22c9594cb39.png

60cb9ee209844d978fd20651436d7ee7.png

文件结构 

1b9d1f77e3c2465a92b8242e614ff7df.png

f8383146592c4740b0bb8fb910013b40.png

1.中文和英文的错误消息配置

bfd2cba4de2e4c4e987e3dbbc343d3aa.png

package com.ldj.mybatisflex.common;import lombok.Getter;/*** User: ldj* Date: 2025/1/12* Time: 17:50* Description: 异常消息枚举*/
@Getter
public enum ExceptionEnum {//# code命名规则:模块编码 + 唯一序号 11表示用户管理模块LOGIN_EXCEPTION(1101),OTHER_EXCEPTION(1102);private Integer code;ExceptionEnum(Integer code) {this.code = code;}
}

2.统一响应类 

package com.ldj.mybatisflex.common;import lombok.Getter;
import lombok.Setter;
import org.springframework.context.i18n.LocaleContextHolder;import java.util.ResourceBundle;/*** User: ldj* Date: 2025/1/12* Time: 18:08* Description: 统一响应类*/
@Getter
@Setter
public class Response<T> {private static final String basePath = "i18n/message";private static final Integer successCode = 200;private static final String success = "成功!";private static final Integer failCode = 500;private static final String fail = "失败!";private Integer code;private String message;private T data;public static <T>Response<T> success(T data) {Response<T> response = new Response<>();response.setCode(200);response.setMessage(success);response.setData(data);return response;}public static <T>Response<T> fail() {return fail(failCode, fail);}//关键代码是读取国际化的配置文件,作为错误提示消息public static <T>Response<T> fail(ExceptionEnum exceptionEnum, T date) {Response<T> response = new Response<>();response.setCode(exceptionEnum.getCode());ResourceBundle bundle = ResourceBundle.getBundle(basePath, LocaleContextHolder.getLocale());String message = bundle.getString(exceptionEnum.getCode().toString());response.setMessage(message);response.setData(date);return response;}public static <T>Response<T> fail(Integer code, String message) {Response<T> response = new Response<>();response.setCode(code);response.setMessage(message);response.setData(null);return response;}
}

 3.登录拦截器

package com.ldj.mybatisflex.interceptor;import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.i18n.SimpleLocaleContext;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;/*** User: ldj* Date: 2025/1/12* Time: 21:05* Description: 登录拦截器*/
@Order(1)
@Component
public class LoginUserInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//需要前端传过来的,这里一直是nullString language = request.getHeader("language");//假设前端传"en_US"//language = "en_US";//默认是zh_CNif (language == null || "".equals(language) || "zh_CN".equalsIgnoreCase(language)) {LocaleContext localeContext = new SimpleLocaleContext(Locale.SIMPLIFIED_CHINESE);LocaleContextHolder.setLocaleContext(localeContext);}else {LocaleContext localeContext = new SimpleLocaleContext(Locale.US);LocaleContextHolder.setLocaleContext(localeContext);}System.out.println("配置语言:" + LocaleContextHolder.getLocale());return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {//请求结束后记得清理 ThreadLocal里面的值,好让垃圾回收器回收LocaleContextHolder.resetLocaleContext();}
}

 4.配置webmvc 应用登录拦截器

package com.ldj.mybatisflex.config;import com.ldj.mybatisflex.interceptor.LoginUserInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** User: ldj* Date: 2025/1/12* Time: 21:00* Description: No Description*/
@Configuration
public class WebConfig implements WebMvcConfigurer {@Autowiredprivate LoginUserInterceptor loginUserInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(loginUserInterceptor);}
}

5.测试

package com.ldj.mybatisflex.controller;import com.ldj.mybatisflex.common.ExceptionEnum;
import com.ldj.mybatisflex.common.Response;
import com.ldj.mybatisflex.model.dao.UerInfoDAO;
import com.ldj.mybatisflex.model.dto.UserLoginReqDTO;
import org.springframework.web.bind.annotation.*;import javax.validation.Valid;/*** User: ldj* Date: 2024/11/17* Time: 16:55* Description: No Description*/
@RestController
@RequestMapping("/userInfo")
@CrossOrigin(maxAge = 3600)
public class UserInfoController {@PostMapping(value = "/login")public Response<UerInfoDAO> login(@RequestBody @Valid UserLoginReqDTO userLoginReqDTO){return Response.fail(ExceptionEnum.LOGIN_EXCEPTION, null);}
}

总结,这有个问题,每次读取配置文件都要操作io流,性能比直接定义在枚举差,3个字段,一个code ,1个cnMsg  ,1个enMsg,再写个方法判断是否中,英获取消息。

但是如果是法语,就失去灵活性,要改代码

 


http://www.mrgr.cn/news/83541.html

相关文章:

  • “AI智能服务平台系统,让生活更便捷、更智能
  • 泛目录和泛站有什么差别
  • 大模型评测
  • 再次梳理ISP的大致流程
  • git问题
  • UML系列之Rational Rose笔记三:活动图(泳道图)
  • Spring MVC简单数据绑定
  • PyQt5按钮类控件Button
  • 信息科技伦理与道德3:智能决策
  • Picocli 命令行框架
  • Virsh虚拟机连接校园网
  • Elasticsearch:使用 Playground 与你的 PDF 聊天
  • 51c~Pytorch~合集5
  • 宝塔面板 php8.0 安装 fileinfo 拓展失败
  • 继续以“实用”指导Pythonic编码(re通配表达式)(2024年终总结2)
  • Android系统定制APP开发_如何对应用进行系统签名
  • LeetCode热题100-环形链表【JavaScript讲解】
  • 补种未成活胡杨
  • 对话新晋 Apache SeaTunnel Committer:张圣航的开源之路与技术洞察
  • HarmonyOS Next系列之华为账号一键登录功能实现(十四)
  • shell脚本回顾1
  • 【Ubuntu 24.04】虚拟机常见问题解决
  • rk3568 内核态OOM内存泄漏memleak使用
  • 分类模型为什么使用交叉熵作为损失函数
  • Spring——几个常用注解
  • mybatis分页插件:PageHelper、mybatis-plus-jsqlparser(解决SQL_SERVER2005连接分页查询OFFSET问题)