在代码下找不到信息…适用于区域设置 en_US。

huangapple go评论83阅读模式
英文:

No message found under code... for locale en_US

问题

我知道...这种问题已经被其他用户在整个StackOverflow上报告过。尽管如此,我尝试了许多不同的解决方案,但都没有成功。我正在使用spring-boot编写一个REST微服务,并在文件夹"/src/main/resources"下加载我的消息。

在同一个文件夹中有三个不同的消息文件:

  • messages.properties;
  • messages_en_US.properties
  • messages_pt_BR.properties

我的Bean验证消息映射如下:

@NotBlank(message = "{customer.input.password.notblank}")
@Length(max = 12, message = "{customer.input.password.maxlength}")
@NotNull(message = "{customer.input.password.notnull}")
@ValidPassword(message = "{customer.input.password.invalid}")
private String password;

我的MessageSource Bean 在主Spring Boot类中进行了定义:

@Bean(name = "messageSource")
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    Locale.setDefault(Locale.US);
    messageSource.setBasename("/WEB-INF/messages");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

我有一个负责从文件获取消息的实用类:

package com.rumblesoftware.utils;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

@Component
public class PostOfficer {

    @Autowired
    @Qualifier(value = "messageSource")
    private MessageSource ms;

    public String getMessage(String messageId) {
        return ms.getMessage(messageId, null, Locale.getDefault());
    }
}

最后,当我尝试使用无效密码时出现的错误消息如下:

org.springframework.context.NoSuchMessageException: No message found under code '{customer.input.password.invalid}' for locale 'en_US'.
    at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:161) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
英文:

I know... this kind of issue was already reported by other users all over the stackOverflow. Even though, I've been trying a lot of different solutions without success. I'm coding a rest microservice using spring-boot and loading my messages under the folder: "/src/main/resources"

There are three different message files in the same folder:

messages.properties;
messages_en_US.properties
messages_pt_BR.properties

My bean validation message is mapped this way:

@NotBlank(message = "{customer.input.password.notblank}")
@Length(max = 12,message="{customer.input.password.maxlength}")
@NotNull(message="{customer.input.password.notnull}")
@ValidPassword(message = "{customer.input.password.invalid}")
private String password;

My MessageSource bean is defined in my main spring boot class.

@Bean(name="messageSource")
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    Locale.setDefault(Locale.US);
    messageSource.setBasename("/WEB-INF/messages");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

I've an utility class responsible to get the messages from the file:

package com.rumblesoftware.utils;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

@Component
public class PostOfficer {

	@Autowired
	@Qualifier(value = "messageSource")
	private MessageSource ms;
	
	public String getMessage(String messageId) {
		return ms.getMessage(messageId,null,Locale.getDefault());
	}
}

finally the error message when i try an invalid password

org.springframework.context.NoSuchMessageException: No message found under code '{customer.input.password.invalid}' for locale 'en_US'.
	at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:161) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]

答案1

得分: 2

我终于找到了问题出在哪里。实际上,代码是正确的,但错误消息标识符在传递时带有"{" "}",而我只需要这样做:

@NotBlank(message = "customer.input.password.notblank")
@Length(max = 12,message="customer.input.password.maxlength")
@NotNull(message="customer.input.password.notnull")
@ValidPassword(message = "customer.input.password.invalid")
英文:

I have finally found out what was going wrong. Actually, the code was right but the error message identifier was being passed with "{" "}" when I just had to do this way:

@NotBlank(message = "customer.input.password.notblank")
@Length(max = 12,message="customer.input.password.maxlength")
@NotNull(message="customer.input.password.notnull")
@ValidPassword(message = "customer.input.password.invalid")

huangapple
  • 本文由 发表于 2020年10月5日 02:00:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64198082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定