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
-
WeChat's Official Account Launches "Author Read Aloud Voice" Feature for Personalized Article ListeningDetail
2024-12-18 17:19:57 1
-
The 12th China University Students' Polymer Materials Innovation and Entrepreneurship Competition Finals Grand Opening in Guangrao CountyDetail
2024-12-18 16:04:28 1
-
Tracing the Ancient Shu Road, Winds of the Three Kingdoms: Global Influencer Shu Road Journey LaunchesDetail
2024-12-18 15:23:35 1
-
Seres: A Pioneer in ESG Practices, Driving Sustainable Development of China's New Energy Vehicle IndustryDetail
2024-12-17 16:20:26 1
- Detail
-
My Health, My Guard: Huawei WATCH D2 Aids Precise Blood Pressure Management in the Winter Health BattleDetail
2024-12-17 09:36:15 1
-
Investigation into the Chaos of Airline Seat Selection: Paid Seat Selection, Seat Locking Mechanisms, and Consumer Rights ProtectionDetail
2024-12-15 16:45:48 1
-
Japanese Scientists Grow Human Organs in Pigs: A Balancing Act of Breakthrough and EthicsDetail
2024-12-14 19:48:50 1
-
Pang Donglai and Sam's Club: Two Paths to Transformation in China's Retail IndustryDetail
2024-12-14 17:57:03 1
-
In-Depth Analysis of China's Precision Reducer Industry: Technological Innovation and Market CompetitionDetail
2024-12-14 16:04:26 1
-
Alibaba's "TAO" App Launches in Japan, Targeting High-Quality Service and Convenient LogisticsDetail
2024-12-13 13:22:23 1
-
In-depth Analysis of China's Cross-border E-commerce Industry Chain: Opportunities and Challenges CoexistDetail
2024-12-13 11:37:17 1
-
Sweet Potato Robotics: How a Unified Software and Hardware Computing Platform Accelerates Robotics Industry DevelopmentDetail
2024-12-13 06:36:34 1
- Detail
-
Yang Liwei: From China's First Taikonaut to a Cornerstone of the Space ProgramDetail
2024-12-12 03:27:26 1
- Detail
- Detail
-
12306 Official Debunks 90-Day Advance Booking for Spring Festival Travel Rush: Beware of ScamsDetail
2024-12-12 02:01:05 1
-
Avoiding TV Buying Traps: A Deep Dive into 4K, HDR, 120Hz, and Other Key SpecificationsDetail
2024-12-11 22:45:54 1
-
NVIDIA's Q3 FY25 Earnings Report: Revenue Surges Past $35 Billion, Setting a New RecordDetail
2024-12-11 21:48:21 1