Seamless integration of multiple languages: the ultimate guide to parameter verification (1)!
AD |
SpringBoot /** * */ @Email(message = "") @NotBlank(message = "") @ApiModelProperty(notes = "", required = true) private String accountName; APP idea LoginBo@Datapublic class LoginBo { /** * */ @NotBlank(message = "") private String accountName; /** * */ @NotBlank(message = "") private String password;}ResultVopublic class ResultVo<T> { private String code; private String msg; private T data; public ResultVo() { } public ResultVo(String code, String msg) { this(code, msg, null); } public ResultVo(String code, String msg, T data) { this.code = code; this

SpringBoot
/** * */ @Email(message = "") @NotBlank(message = "") @ApiModelProperty(notes = "", required = true) private String accountName;
APP
idea

LoginBo
@Datapublic class LoginBo { /** * */ @NotBlank(message = "") private String accountName; /** * */ @NotBlank(message = "") private String password;}
ResultVo
public class ResultVo<T> { private String code; private String msg; private T data; public ResultVo() { } public ResultVo(String code, String msg) { this(code, msg, null); } public ResultVo(String code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; } // }
TestController
@RestController@RequestMapping("/test")public class TestController { @PostMapping("/demo") public ResultVo<Void> demo(@RequestBody @Validated LoginBo bo) { System.out.println(bo); return ResultVo.success(); }}
GlobalExceptionHandler
@Component@RestControllerAdvicepublic class GlobalExceptionHandler { /** * * * @param e BindException * @return ResultVo<Void> */ @ExceptionHandler(BindException.class) public ResultVo<Void> handlerBindException(BindException e) { return ResultVo.failure(this.buildMsg(e.getBindingResult())); } /** * * * @param e MethodArgumentNotValidException * @return ResultVo<Void> */ @ExceptionHandler(MethodArgumentNotValidException.class) public ResultVo<Void> handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) { return ResultVo.failure(buildMsg(e.getBindingResult())); } /** * * * @param bindingResult BindingResult * @return String */ private String buildMsg(BindingResult bindingResult) { StringBuilder builder = new StringBuilder(32); for (FieldError error : bindingResult.getFieldErrors()) { builder.append(", [").append(error.getField()).append(":").append(error.getDefaultMessage()).append("]"); } return builder.substring(2); }}
- message
Spring Boot messages.properties messages_zh_CN.properties
- messages_zh_CN
account.name=password=
- messages_en_US
account.name=account name cannot be emptypassword=password cannot be empty
@Datapublic class LoginBo { /** * */ @NotBlank(message = "{account.name}") private String accountName; /** * */ @NotBlank(message = "{password}") private String password;}


@Configurationpublic class ValidationConfig { @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); // messageSource.setBasename("messages_zh_CN"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } @Bean public LocalValidatorFactoryBean validator(MessageSource messageSource) { LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean(); validatorFactoryBean.setValidationMessageSource(messageSource); return validatorFactoryBean; }}


@Configurationpublic class ValidationConfig { @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages_en_US"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } @Bean public LocalValidatorFactoryBean validator(MessageSource messageSource) { LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean(); validatorFactoryBean.setValidationMessageSource(messageSource); return validatorFactoryBean; }}


# server: port: 10000# app: default: language: zh_CN
@Slf4j@Configurationpublic class ValidationConfig { @Value("${app.default.language}") private String defaultLanguage; @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages_" + defaultLanguage); messageSource.setDefaultEncoding("UTF-8"); log.info("Message Source init suc -> lang:{}", defaultLanguage); return messageSource; } @Bean public LocalValidatorFactoryBean validator(MessageSource messageSource) { LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean(); validatorFactoryBean.setValidationMessageSource(messageSource); return validatorFactoryBean; }}

setBasename


basename ResourceBundle
@Beanpublic MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages_en_US"); messageSource.setDefaultEncoding("UTF-8"); return messageSource;}
@Beanpublic MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource;}
basename ResourceBundle
ResourceBundle
ResourceBundle Java Locale
- LocaleResourceBundle
- ResourceBundle
- ResourceBundle ResourceBundle Locale
Locale
Spring Boot Locale Locale.getDefault() Locale.getDefault() JVM Locale
Spring Boot Locale JVM Spring Boot Locale
basename basename ResourceBundle ResourceBundle Spring Boot Locale
ResourceBundle {xx.xxx}
@Slf4j@Configurationpublic class ValidationConfig { @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } @Bean public LocalValidatorFactoryBean validator(MessageSource messageSource) { LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean(); validatorFactoryBean.setValidationMessageSource(messageSource); return validatorFactoryBean; }}
spring boot Locale JVM
LocaleResolver
@Slf4j@Configurationpublic class ValidationConfig { @Value("${app.default.language}") private String defaultLanguage; @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } @Bean public LocaleResolver localeResolver() { SessionLocaleResolver resolver = new SessionLocaleResolver(); resolver.setDefaultLocale(new Locale(defaultLanguage)); return resolver; } @Bean public LocalValidatorFactoryBean validator(MessageSource messageSource) { LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean(); validatorFactoryBean.setValidationMessageSource(messageSource); return validatorFactoryBean; }}

Spring BootLocale
x-langnacosmysql
Disclaimer: The content of this article is sourced from the internet. The copyright of the text, images, and other materials belongs to the original author. The platform reprints the materials for the purpose of conveying more information. The content of the article is for reference and learning only, and should not be used for commercial purposes. If it infringes on your legitimate rights and interests, please contact us promptly and we will handle it as soon as possible! We respect copyright and are committed to protecting it. Thank you for sharing.(Email:[email protected])
Mobile advertising space rental |
Tag: Seamless integration of multiple languages the ultimate guide to
Behind the vulgar chaos of live streaming: Several platforms have been named multiple times, and scholars suggest improving the disciplinary mechanism
NextSonova Presents at the Beijing International Audiology Conference with the Latest Hearing Aid Technology from Unikang
Guess you like
-
Pinduoduo's "Trillion-Yuan Support" Plan: A Three-Year, 100 Billion Yuan Investment to Build a Multi-Win Business EcosystemDetail
2025-04-03 14:41:29 11
-
Huyu Xianxiang and AVIC Optoelectronics Institute Forge Strategic Partnership to Shape China's eVTOL Avionics LandscapeDetail
2025-04-02 18:39:02 1
-
Haier Smart Home's 8th Global R&D Innovation Awards: Illuminating Better Lives with Technology, Achieving User SatisfactionDetail
2025-04-02 15:57:33 21
-
Huawei's 2025 China Digital Power Partner Conference: Carbon-Neutral Path for China, Shared Value CreationDetail
2025-03-31 18:57:09 11
-
OPPO Think Tank: A New Paradigm for Chinese Enterprises' Globalization From Wusha Village to the Global High-End MarketDetail
2025-03-31 18:48:21 1
-
ICLR 2025: Chinese Universities and Companies Showcase AI Prowess with Numerous Accepted Papers; Stanford-HKUST Collaboration Achieves Perfect ScoreDetail
2025-03-31 14:54:45 11
-
Huawei HarmonyOS Smart Home Partner Summit: Deep Dive into Spatial Intelligence Transformation and Ecosystem Development StrategyDetail
2025-03-31 13:01:45 1
-
AI Large Models Drive Innovation in Humanoid Robots and Autonomous Driving: 2025 as a Key MilestoneDetail
2025-03-31 13:00:04 11
-
Eight Cities Pilot Credit Supervision Data Openness, Empowering Micro and Small Enterprises with Mobile Payment PlatformsDetail
2025-03-26 09:32:47 1
-
Xiaomi's "Just a Little Profit": The Deep Logic and Sustainability Behind its Low-Margin StrategyDetail
2025-03-25 15:07:32 21
- Detail
-
The Ninth Huawei ICT Competition China Challenge Finals Conclude Successfully: Kunpeng and Ascend Tracks Crown Their ChampionsDetail
2025-03-24 16:26:03 11
-
Ronshen Sugar Cube Refrigerator: The Official Product of the 2025 FIFA Club World Cup, Ushering in a New Era of Healthy Food PreservationDetail
2025-03-24 15:40:35 21
-
Zhihu Launches New Version of Zhihu Straight Answer: Deep Integration of AI and Community to Enhance Professionalism and CredibilityDetail
2025-03-24 14:04:38 1
-
China Construction Ninth Harmony (Zhongjian Jiuhe) and Huawei HarmonyOS Smart Home Deepen Strategic Partnership at AWE2025, Building a Green and Intelligent Future HomeDetail
2025-03-23 15:21:15 41
-
ZuoYeBang Books Leads the New Trend in Intelligent Education Publishing at Changsha Book FairDetail
2025-03-21 15:15:33 1
-
Tianyancha: Shielding Consumer Safety and Reshaping Business Trust with DataDetail
2025-03-21 08:47:58 1
-
Hisense at AWE2025: AI Empowerment, Leading the Transformation of Future Smart LivingDetail
2025-03-20 18:24:11 11
-
Haier TV Makes a Stunning Debut at AWE 2024: Zhiyuan AI Large Model and PureScene Care Screen Usher in a New Era of Smart HomesDetail
2025-03-20 15:17:20 1
-
China Power's Xin Yuan Zhi Chu (New Source Smart Storage): Open Energy Intelligence Computing Center Leads Intelligent Transformation of the Energy IndustryDetail
2025-03-20 15:15:39 1