Home Page >  News List >> Tech >> Tech

Seamless integration of multiple languages: the ultimate guide to parameter verification (1)!

Tech 2023-05-29 21:02:30 Source: Network
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);    }}

  1. message

Spring Boot messages.properties messages_zh_CN.properties

  1. messages_zh_CN
account.name=password=
  1. 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

  1. LocaleResourceBundle
  2. ResourceBundle
  3. 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

Unite directoryCopyright @ 2011-2024 All Rights Reserved. Copyright Webmaster Search Directory System