简体   繁体   English

javax.money抛出javax.money.MonetaryException:AmountFormatQuery没有MonetaryAmountFormat

[英]javax.money throws javax.money.MonetaryException: No MonetaryAmountFormat for AmountFormatQuery

I've got an existing servlet application using tomcat 8 and java 8. I've been using org.javamoney.moneta.Money for some time. 我有一个使用tomcat 8和Java 8的现有servlet应用程序。我已经使用org.javamoney.moneta.Money已有一段时间了。

Today I wanted to add a new pattern and I started getting the following exception: 今天,我想添加一个新的模式,并且开始出现以下异常:

javax.money.MonetaryException: No MonetaryAmountFormat for 
AmountFormatQuery AmountFormatQuery (
{pattern=$0.00, Query.formatName=default, 
org.javamoney.moneta.format.CurrencyStyle=NAME, java.util.Locale=en})
at javax.money.spi.MonetaryFormatsSingletonSpi.getAmountFormat(MonetaryFormats
SingletonSpi.java:71) ~[money-api-1.0.3.jar:1.0.3]
at javax.money.format.MonetaryFormats.getAmountFormat(MonetaryFormats.java:112) ~[money-api-1.0.3.jar:1.0.3]
    at au.org.noojee.auditor.util.Formatters.format(Formatters.java:92) ~[classes/:?]
at au.org.noojee.auditor.util.Formatters.format(Formatters.java:81) ~[classes/:?]
at au.org.noojee.auditor.entities.Customer.getNotices(Customer.java:242) ~[classes/:?]
at au.org.noojee.auditor.entities.Customer.getWorstError(Customer.java:307) ~[classes/:?]

So I reverted to the original pattern but the exception continues. 因此,我恢复为原始模式,但异常继续。

I've traced the code through and the problem appears to stem from the fact that the following call to getServices returns an empty list. 我已经跟踪了代码,问题似乎源于以下对getServices的调用返回空列表这一事实。

Bootstrap.getServices(MonetaryAmountFormatProviderSpi.class) 

So the strange thing is that if I set up a unit test in the same project everything works fine. 因此,奇怪的是,如果我在同一个项目中设置了单元测试,那么一切都会很好。

I've now upgraded to money 1.3 but I'm getting the same errors (and the unit test still works). 我现在已经升级到1.3版,但是出现了同样的错误(并且单元测试仍然有效)。

The code that fails (when running under tomcat) is: 失败的代码(在tomcat下运行时)是:

Note: I hard coded the amount in case the passed money1 was causing an issue (but of course the exception is thrown before its used so wouldn't be the issue anyway). 注意:我对金额进行了硬编码,以防万一通过的money1引起了问题(但当然,在使用它之前会引发异常,因此无论如何都不会成为问题)。

public static String format(Money money1, String pattern)
{
    MonetaryAmount money = Money.of(12345.67, "AUD");
    MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(
            AmountFormatQueryBuilder.of(Locale.ENGLISH)
            .set(CurrencyStyle.NAME)
            .set("pattern", "$0.00")
            .build()); 


    String result;
    if (money == null)
        result = "";
    else
        result = customFormat.format(money);
    return result;

}

The Unit test that works is: 起作用的单元测试是:

static public final CurrencyUnit LOCAL_CURRENCY = Monetary.getCurrency(Locale.getDefault());

@Test
public void test()
{
    MonetaryAmount amount = Money.of(12345.67, "AUD");
    MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(
            AmountFormatQueryBuilder.of(Locale.ENGLISH)
            .set(CurrencyStyle.NAME)
            .set("pattern", "$0.00")
            .build()); 

            String formatted = customFormat.format(amount); //00,01,23,45.67 US Dollar
            System.out.println(formatted);
}

Note: the call to getCurrency is also in the tomcat servlet app but in a different class. 注意:对togetCurrency的调用也在tomcat servlet应用程序中,但在另一个类中。 I've ran the unit test with and without that line but no differences. 我已经运行了带有和不带有该行的单元测试,但是没有差异。

I've checked my pom in case there were any conflicts but it reports that moneta 1.3 and money-api 1.0.3 are in use which I belive is correct. 我检查了pom以防有任何冲突,但是它报告说moneta 1.3和money-api 1.0.3正在使用,我相信是正确的。

I've rebooted my dev environment incase something went funny with the systems locale settings (but that makes no sense anyway given the unit test works). 我已经重新启动了我的开发环境,以防某些与系统区域设置有关的事情发生了(但是考虑到单元测试的有效性,这还是毫无意义的)。

Any hints where to look would be greatly appreciated. 任何提示在哪里将不胜感激。

Not a solution but I did develop a work around: 不是解决方案,但我确实围绕以下方面开展工作:

public static String format(Money money)
{
    long cents = money.scaleByPowerOfTen(2).getNumber().longValueExact();

    return String.format("$%,1d.%02d", cents / 100, cents % 100);

}

The result: 结果:

MonetaryAmount amount = Money.of(99999125.67, "AUD");

System.out.println(format(amount));

$99,999,125.67 $ 99,999,125.67

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM