I have a spring controller which is taking multiple BigDecimal
RequestParams.
My application locale is en_US but just for this controller method I need to bind and convert these BigDecimal
parameters in de_DE locale (ie. #.###,## > DOT for grouping and COMMA for decimal separator).
These BigDecimal
values are coming from the UI text boxes and they are already in the de_DE
format. Here is my controller code which is failing with the following error:
"Failed to convert value of type 'java.lang.String' to required type 'java.math.BigDecimal'; nested exception is java.lang.NumberFormatException"
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView create(@RequestParam("referenceNumber") String referenceNumber, @RequestParam("startDate") @DateTimeFormat(pattern="dd-MM-yyyy") Date startDate, @RequestParam("amount1") @NumberFormat(pattern = "#.###,##") BigDecimal amount1, @RequestParam("amount2") @NumberFormat(pattern = "#.###,##") BigDecimal amount2) {
// Do something and return
}
Spring somehow ignores my numberformat pattern. Please note that DateTimeFormat
annotation works as expected; parsing the startDate parameter in correct form.
Any help would be appreciated.
Thanks.
I solved my problem with:
@RequestParam(value="amount1", required=false) @NumberFormat(pattern="#0,00") BigDecimal amount1
You can use PropertyEditorSupport to handle the form input as follows:
Create class extending
PropertyEditorSupport
to convert String received from client to BigDecimal, for example:And bind it with the controller, as: