MissingResourceException - 找不到基本名称的包

发布于 2024-09-11 06:57:02 字数 1837 浏览 1 评论 0原文

我知道在 stackoverflow 和其他论坛上有很多关于此错误的问题和答案。但我仍然找不到解决方案......

出于速度原因,我有一个实用程序类,它根据提供的区域设置加载所有静态数据映射(例如几个月)。

所以这个实用程序类看起来像这样:

public static final String GLOBAL_MESSAGES = "globalMessages";

private static Map<Integer,Month> monthsMap;

private ResourceBundle getResourceBundle(Locale locale) {
    ResourceBundle rb = ResourceBundle.getBundle(GLOBAL_MESSAGES, locale);
    return rb;
}

private Map<Integer,Month> getMonths() {
    if(monthsMap == null) {
        setMonths();
    }
    return monthsMap;
}

private void setMonths() {
    try {
        monthsMap = getFactory().getDAO().getAllMonths();
    } catch (SQLException e) {
        logger.error(e);
    } catch (EmptyResultException e) {
        logger.error(e);
    }
}

public Map<Integer,Month> getMonths(Locale locale) {
    if(locale == null) {
        return monthsMap;
    } else {
        if(this.locale != locale) {
            this.locale = locale;
            setMonths();
        }
    }
    ResourceBundle rb = getResourceBundle(locale);
    Map<Integer,Month> map = new HashMap<Integer, Month>();
    for(Month akVO : getMonths().values()) {
        try {
            akVO.setName(rb.getString(akVO.getName()));
        } catch (MissingResourceException e) {
            //already done
        }
        map.put(akVO.getId(), akVO);
    }       
    return map;
}

文件globalMessages.properties(globalMessages_en_US.properties,...)直接位于源包resources中。当部署在 Tomcat 上时,位于文件夹 WEB-INF/classes 中。

现在问题来了。 在此应用程序中工作时一切正常。但我有另一个应用程序通过 REST API (JAX-RS) 连接到这个应用程序。发出请求时 App/rest/months.xml 我收到以下错误:

java.util.MissingResourceException: Can't find bundle for base name globalMessages, locale en_us

我真的迷路了。并且绝望...

I know that there are a lot of questions and answers exactly about this error on stackoverflow and other forums. But I still can not find the solution...

For speed reasons I have one utility class which loads all static data maps (for example months) depending on locale provided.

So this utility class looks something like this:

public static final String GLOBAL_MESSAGES = "globalMessages";

private static Map<Integer,Month> monthsMap;

private ResourceBundle getResourceBundle(Locale locale) {
    ResourceBundle rb = ResourceBundle.getBundle(GLOBAL_MESSAGES, locale);
    return rb;
}

private Map<Integer,Month> getMonths() {
    if(monthsMap == null) {
        setMonths();
    }
    return monthsMap;
}

private void setMonths() {
    try {
        monthsMap = getFactory().getDAO().getAllMonths();
    } catch (SQLException e) {
        logger.error(e);
    } catch (EmptyResultException e) {
        logger.error(e);
    }
}

public Map<Integer,Month> getMonths(Locale locale) {
    if(locale == null) {
        return monthsMap;
    } else {
        if(this.locale != locale) {
            this.locale = locale;
            setMonths();
        }
    }
    ResourceBundle rb = getResourceBundle(locale);
    Map<Integer,Month> map = new HashMap<Integer, Month>();
    for(Month akVO : getMonths().values()) {
        try {
            akVO.setName(rb.getString(akVO.getName()));
        } catch (MissingResourceException e) {
            //already done
        }
        map.put(akVO.getId(), akVO);
    }       
    return map;
}

Files globalMessages.properties (globalMessages_en_US.properties,...) are directly in source package resources. When deployed on Tomcat there are in folder WEB-INF/classes.

Now the problem. It all works when working in this application. But I have another application which connects throught REST API (JAX-RS) to this one. When making a request App/rest/months.xml I get the following error:

java.util.MissingResourceException: Can't find bundle for base name globalMessages, locale en_us

I am really lost. And desperate...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-09-18 06:57:02

好的...发现错误了。一天后...问题是区分大小写的字母。尽管如此,当使用“en_US”设置区域设置时,ResourceBundle(在通过 REST 时)会寻找“en_us”。

编辑
好的,还发现了错误,为什么它都是小写字母。
问题是,因为我创建语言环境时使用:

Locale locale = new Locale("en_US");

而不是:

Locale locale = new Locale("en","US");

Ok... Found the error. After one f* day... The problem is in case sensitive letters. Even though, when setting the locale from rest with "en_US" somehow ResourceBundle (when going through REST) is looking for "en_us".

EDIT:
Ok, found also the error why it was all in small letters.
Problem was, because I was creating locale with:

Locale locale = new Locale("en_US");

instead of:

Locale locale = new Locale("en","US");
情定在深秋 2024-09-18 06:57:02

对于在这种情况下使用 Glassfish 服务器遇到问题的任何人,可以在管理概述中设置区域设置

在此处输入图像描述

简单地覆盖默认区域设置,如图所示

有人可能会发现这很有用,我有麻烦几个小时,不希望任何人也遇到这种问题

For anybody having trouble with a Glassfish server in this case, there is the possibility to set the Locale within the Admin Overview

enter image description here

Simple overwrite the default locale as shown in the picture

Somebody might find this useful, I had trouble for multiple hours and dont want anyone to have this kind of problem aswell

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文