SpringBoot2 国际化最佳实践

2024-05-13 16:48

本文主要是介绍SpringBoot2 国际化最佳实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一般情况下确实对应不到国际化, 但是无奈老板说, 我们要做国际化, 作为boss  drive  develop 的一员, 还能有啥办法, 我也很绝望啊.,那就做呗.

其实很简单, 这里就以我们抛出的异常进行封装, 在英文和中文环境下两套不同的错误.

1. 新建项目

2.在resources 文件夹下建立 i18n文件夹, 放错误信息.

新建文件   error_zh_CN.properties   放置中文错误信息

新建文件  error_en_US.properties   放置中文错误信息

 

3. 新建配置类:

package com.curefun.core.config;import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.i18n.LocaleContextHolder;/** 错误码文本获取* 在项目resources目录下新建i18n文件夹,错误码文本按语言类型划分文件,使用error开头,后跟语言编码,如error_zh_cn.properties,error_en_us.properties*/
@PropertySource(value = { "classpath:i18n/error*.properties" })
public class ErrorCodeMessage {private static ConcurrentHashMap<String, ResourceBundle> messages = new ConcurrentHashMap<String, ResourceBundle>();/*** 获取错误码文本* @param key 错误码编码,如“10000”* @param params String.format()占位符对应文本* @return*/public static String getMessage(String key, Object... params) {// 获取语言,这个语言是从header中的Accept-Language中获取的,// 会根据Accept-Language的值生成符合规则的locale,如zh、pt、en等Locale locale = LocaleContextHolder.getLocale();ResourceBundle message = messages.get(locale.getLanguage());if (null == message) {synchronized (messages) {// 在这里读取配置信息message = messages.get(locale.getLanguage());if (null == message) {message = ResourceBundle.getBundle("i18n/error", locale);messages.put(locale.getLanguage() + "_" + locale.getCountry(), message);}}}// 此处获取并返回messageif (null != params) {return String.format(message.getString(key), params);}return message.getString(key);}/*** 清空错误文本缓存map*/public static void flushMessage() {messages.clear();}
}

这里进行配置的加载, 进行信息的缓存,

 

 

在业务代码中只需要很简单的抛出异常就好了

public class BusinessException extends  RuntimeException {private String code;public BusinessException(String code) {super(ErrorCodeMessage.getMessage(code));this.code = code;}public BusinessException(String code, Object ...msgError) {super(ErrorCodeMessage.getMessage(code,msgError));this.code = code;}public String getCode() {return this.code;}}

 

最后一步, 定义一个全局的异常处理器

 

import com.alibaba.fastjson.JSONObject;
import com.curefun.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;/*** @author zk* @Description: 全局异常处理* @date 2019年10月29日15:08:35*/
@Slf4j
@RestControllerAdvice
public class ExceptionHandleController {/*** 处理参数异常** @param ex*/@ExceptionHandler(BusinessException.class)public JSONObject handleParamException(BusinessException ex) {JSONObject obj = new JSONObject();obj.put("code", ex.getCode());String message = ex.getMessage();obj.put("msg", message);obj.put("data", "");log.error("发生异常:{}",ex);return obj;}/*** 处理参数缺失异常** @param ex*/@ExceptionHandler(MissingServletRequestParameterException.class)public JSONObject handleMissParamException(BusinessException ex) {JSONObject obj = new JSONObject();obj.put("code", 15000);obj.put("msg", "缺少参数");obj.put("data", "缺少参数");log.error("发生异常:{}",ex);return obj;}@ExceptionHandler(Exception.class)public JSONObject handleException(Exception ex) {JSONObject obj = new JSONObject();obj.put("code", "15000");obj.put("msg", "服务内部错误");obj.put("data", "");ex.printStackTrace();log.error("发生异常:{}",ex.getCause());return obj;}}

好像我又水了一篇文章,  哈哈哈 ,  真容易,  果然花了5分钟写的文章 , 大概率是很水的文章.  凑合看下,  非常简单的总结

这篇关于SpringBoot2 国际化最佳实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/986273

相关文章

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

SpringBoot整合easy-es的详细过程

《SpringBoot整合easy-es的详细过程》本文介绍了EasyES,一个基于Elasticsearch的ORM框架,旨在简化开发流程并提高效率,EasyES支持SpringBoot框架,并提供... 目录一、easy-es简介二、实现基于Spring Boot框架的应用程序代码1.添加相关依赖2.添

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录