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
-
The Facial Swelling of Shenzhou 18 Astronauts: The Physiological Cost of Space Exploration and Future ChallengesDetail
2024-11-17 08:03:04 1
-
Xiaomi Automobile Unveils Intelligent Chassis Pre-Research Technology, Ushering in a New Era of "Human-Car-Home Full Ecosystem"Detail
2024-11-14 11:24:27 1
-
Douyin E-commerce Double 11 Data Report: Merchants Businesses Grow, Consumer Trends EmergeDetail
2024-11-14 11:23:11 1
-
New Trends in SOE Reform: Focusing on Five Values to Build a "Living Organism"Detail
2024-11-14 11:19:26 1
-
CATL Chairman Zeng Yuqun: Musk Doesn't Understand Batteries, Tesla's Bet on Cylindrical Batteries is Doomed to FailDetail
2024-11-13 18:47:38 1
-
China Eastern Airlines Technology and Thales Renew Cooperation Agreement, Deepening Avionics Maintenance PartnershipDetail
2024-11-13 16:40:50 1
- Detail
- Detail
- Detail
-
Li Jiaqi's Livestream Double 11 Report: Domestic Brands Surge, Winter Warmer Economy BoomsDetail
2024-11-12 11:07:26 11
-
BYD: Plug-in Hybrids "To the Rescue," Behind the Price War Lies a "Davis Double-Click" in ProfitabilityDetail
2024-11-12 10:49:05 1
-
The Rise of Online Livestreamers: A Mass Career with 15 Million Dream Chasers in Live RoomsDetail
2024-11-11 15:27:33 11
-
Microsoft "Mail and Calendar" app will be officially discontinued at the end of next year, users need to migrate to the new OutlookDetail
2024-11-10 14:53:36 11
- Detail
-
Alibaba Pictures' Phoenix Cloud Intelligence International Edition iCIRENA Expands to Hong Kong and Macau, Bringing Technological Upgrades to CinemasDetail
2024-11-09 11:22:49 11
-
From Daughter of Heaven to Ordinary Mom: Liu Yang's Space Dream and the Diversification of LifeDetail
2024-11-09 10:36:56 1
- Detail
-
Global Focus: CIIE Signs Deals Worth Over 10 Billion, 6G Technology Takes the Lead, Avian Flu Outbreak Ravages, Typhoon "Ginkgo" ApproachesDetail
2024-11-08 14:39:05 1
-
The Battle for the Smartphone Throne: Apple, Samsung, and Huawei Vie for DominanceDetail
2024-11-07 21:01:50 1
-
Why Chinese Astronauts Lie Down When Exiting the Capsule? The Truth is Not InferiorityDetail
2024-11-07 00:51:26 11