Wednesday, May 23, 2012

Comma-separated Numbers to BigDecimal in SpringMVC

One of the cool things about SpringMVC is it allows you to convert special cases to objects. A special case might be trying to convert a number such as 6,117,748.00 to a BigDecimal. If you just try to pass it through normally you'll get
Failed to convert property value of type java.lang.String to required type java.math.BigDecimal for property annualExpectedIncome; nested exception is java.lang.NumberFormatException
Normally we can convert such a number to a BigDecimal with the DecimalFormat object, but instead of changing the type of object we're using in our controller method, lets use the @InitBinder to do the conversion for us.

No comments:

Post a Comment