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
-
China Leads in Developing IEC 63206 International Standard, Driving Global Innovation in Industrial Process Control System RecordersDetail
2025-01-18 11:06:14 1
-
The 2024 Micro-Short Series Industry Ecological Insight Report: 647,000 Job Opportunities, Rise of Diversified Business Models, and High-Quality Content as the Future TrendDetail
2025-01-17 17:33:01 1
-
Global PC Market Shows Moderate Recovery in 2024: High AIPC Prices a Bottleneck, Huge Growth Potential in 2025Detail
2025-01-17 11:02:09 1
-
Bosch's Smart Cockpit Platform Surpasses 2 Million Units Shipped, Showcasing Strength in Intelligent Driving TechnologyDetail
2025-01-17 10:55:29 1
-
YY Guangzhou Awarded "2024 Network Information Security Support Unit" for Outstanding ContributionsDetail
2025-01-17 10:43:28 1
-
TikTok CEO Invited to Trump's Inauguration, Biden Administration May Delay BanDetail
2025-01-16 20:06:11 1
-
Douyin Denies Opening International Registration: Overseas IPs Don't Equate to Overseas Registration; Platform Actively Combats Account ImpersonationDetail
2025-01-16 14:26:12 1
-
Lei Jun, Xiaomi's founder, chairman, and CEO, has set a new goal: learning to drive a forklift!Detail
2025-01-15 10:22:30 11
-
ByteDance Scholarship 2024: Fifteen Outstanding Doctoral Students Awarded RMB 100,000 Each to Advance Frontier Technology ExplorationDetail
2025-01-14 15:56:39 1
-
Fliggy Launches "Peace of Mind for the New Year" Service Initiative to Ensure Smooth Travel During the Year of the Snake Spring Festival RushDetail
2025-01-14 15:24:53 1
-
Arm's Massive Fee Hike and Potential In-House Chip Development: A Precursor to a Seismic Shift in the Chip Industry?Detail
2025-01-14 11:02:36 1
-
Adobe Firefly Launches: Generative AI Suite Revolutionizes Image and Video Processing EfficiencyDetail
2025-01-14 10:46:39 1
-
Chinese New Year Elements Sell Like Hotcakes Overseas: Cross-border E-commerce "Spring Festival Economy" Booms, Cainiao Overseas Warehouses Help Merchants Capture Market ShareDetail
2025-01-13 14:17:50 1
-
China Railway's 12306 System Successfully Navigates Spring Festival Travel RushDetail
2025-01-13 12:56:54 1
-
Handan, Hebei Province Successfully Tests First Low-Altitude Drone Delivery Route, Ushering in a New Era of Smart LogisticsDetail
2025-01-13 12:50:13 1
-
Kuaishou Leads in Developing Anti-Fraud Industry Standards, Contributing to a Secure and Reliable Short-Video CommunityDetail
2025-01-13 09:47:32 11
-
Microsoft Offers Top Salaries to Retain AI Talent: AI Software Engineers Earn Over $400,000 AnnuallyDetail
2025-01-12 17:28:34 11
- Detail
-
Chang'e-5 Mission Unveils Secrets: New Discoveries Regarding Lunar Magnetic Field Strength and Deep Dynamics 2 Billion Years AgoDetail
2025-01-10 11:42:44 11
-
SenseTime's "Day Day New" Multimodal Large Model: Native Fusion Enables Diverse ApplicationsDetail
2025-01-10 11:40:40 21