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
- Detail
- Detail
-
Ant Group Powers the Greater Bay Area's "One-Hour Living Circle" and Fuels Global "ChinaTravel Boom"Detail
2024-11-21 19:23:04 1
-
Shenzhen's First Roadside Supercharger Station Commences Trial Operation, Ushering in a New Era for the "Supercharging City"Detail
2024-11-21 11:25:06 1
-
Xiaomi's High-End Strategy: An In-Depth Analysis of Q3 2024 Financial Results and Future OutlookDetail
2024-11-19 23:07:40 1
-
TSMC's Sudden Shift: A Global Chip Giant's Difficult Choices in the US-China GameDetail
2024-11-19 12:27:48 1
-
International Space Station Leak Crisis: NASA's Emergency Evacuation Plan and Signals of Chinese CooperationDetail
2024-11-19 11:34:51 1
-
Ten Years of Searching: Li Eryou's Unwavering Hope in the Search for His Son on MH370Detail
2024-11-18 18:39:16 1
-
The Facial Swelling of Shenzhou 18 Astronauts: The Physiological Cost of Space Exploration and Future ChallengesDetail
2024-11-17 08:03:04 11
-
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 11
-
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