开发者

What Java type to use to represent a currency value in a web service?

I'm creating a web service in Java that will be consumed by an external application, probably written in C#. In my Purchase bean, I have a Currency object for the total cost. However, this开发者_运维问答 leads to the following error:

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.Currency does not have a no-arg default constructor.

I found a solution to create a custom XML Adapter to handle the Currency marshalling/unmarshalling:

public class CurrencyAdapter extends XmlAdapter<String,Currency> {
  public Currency unmarshal(String val) throws Exception {
    return Currency.getInstance(val);
  }
  public String marshal(Currency val) throws Exception {
    return val.toString();
  }
}

Could I use that custom XML Adapter, or use a BigDecimal (or other type) object to represent the cost?


You are conflating a currency (which is a named unit) and a cost (which is some quantity in that unit). You can't represent a Currency as a BigInteger, because a Currency is not a quantity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜