I've got an existing servlet application using tomcat 8 and java 8. I've been using org.javamoney.moneta.Money for some time.
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.
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).
The code that fails (when running under tomcat) is:
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).
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. 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.
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.