简体   繁体   English

如何发送messages.properties键到在控制器中引发的异常?

[英]How to send a messages.properties key to an exception being thrown in a controller?

I am trying to implement custom messages with a different language in my application built with Spring Boot 2. So far I have implemented it for entities' fields using keys set in messages_br.properties . 我正在尝试在使用Spring Boot 2构建的应用程序中使用另一种语言来实现自定义消息。到目前为止,我已经使用messages_br.properties设置的键为实体字段实现了它。 Now, I'm trying the same with exception messages thrown in controllers, but I don't understand how to do it. 现在,我正在尝试对控制器中引发的异常消息进行相同的操作,但是我不知道该怎么做。 Examples show too much of things I believe is not what I need. 例子显示了我认为不是我需要的太多东西。

To configure the file, this is in my Application.java : 要配置文件,这是在我的Application.java

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource bundleMessageSource = new ReloadableResourceBundleMessageSource();
    bundleMessageSource.setBasename("classpath:messages_br");
    bundleMessageSource.setDefaultEncoding("UTF-8");

    return bundleMessageSource;
}

@Bean
public LocalValidatorFactoryBean getValidator() {
    LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
    bean.setValidationMessageSource(messageSource());

    return bean;
}

In the annotations above each field, I can specify something like this: 在每个字段上方的注释中,我可以指定以下内容:

@NotEmpty(message = "{beneficiary.name.notEmpty}")
private String name;

But the same format in a thrown exception prints that only: 但是在抛出的异常中使用相同的格式只能打印出以下内容:

throw new InsufficientBalanceException("${user.balance.insufficientBalance}");

I'm almost sure that when learning how to implement exceptions I saw a similar example but now I cannot find it anymore nor I remember how it is done. 我几乎可以肯定,在学习如何实现异常时,我看到了一个类似的示例,但是现在我找不到了,也不记得它是如何完成的。

You can create a property for error message with @Value , 您可以使用@Value创建错误消息的属性,

@Value("${user.balance.insufficientBalance}")
private String insufficientBalance;

And then use it in Exception, 然后在Exception中使用它,

throw new InsufficientBalanceException(insufficientBalance);

If you don't want to create extra property, you can use Environment 如果您不想创建额外的属性,则可以使用Environment

@Configuration
@PropertySource("file:messages_br.properties")
public class ApplicationConfiguration {
    @Autowired
    private Environment env;
    public void getErrorMessage() {
        env.getProperty("insufficientBalance");
    }
    //then use getErrorMessage
    throw new InsufficientBalanceException(getErrorMessage);
   }
} 

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在注释中读取messages.properties - how to read messages.properties in Annotations 清理messages.properties - clean messages.properties Spring Boot:添加了外部messages.properties但未使用 - Spring Boot: external messages.properties are being added but not used 在spring message.properties中,如何在使用错误代码作为键时确保错误消息的换行符? - In a spring messages.properties, how to ensure line break of error message when using an error code as key? 如何将messages.properties文件移动到单独的文件夹中? - How to move messages.properties files into a separate folder? 如何在messages.properties文件中使用命名参数? - How can I used named parameters in a messages.properties file? 在Grails中,如何获取locale的ConfigObject of messages.properties? - In Grails, how can I get a ConfigObject of messages.properties for locale? 如何将消息从messages.properties添加到捕获? - How do I add a message from a messages.properties to a catch? 如何自动在Java / Spring中重新加载messages.properties文件? - How to automatically reload messages.properties files in Java/Spring? 如何在java包(Spring Boot)中加载`messages.properties`? - How to load `messages.properties` in java packages (Spring Boot)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM