Skip to content
Advertisement

Convert String to Money Thymeleaf

I am having a problem converting input String to money. I am creating a spring boot application with thymeleaf. I have a web page where user inputs the data and there is a particular field where he inputs the String and it needs to be converted to type joda.money as in my pojo class that field has money data type. This is a full governor_form.html:

JavaScript

This is where he inputs that data:

JavaScript

So if i’m not mistaken Hibernate is having a problem converting a String input from thymeleaf to type money as I get the following error:

JavaScript

This is the joda.money dependency:

JavaScript

This is a POJO class:

JavaScript

This is a controller:

JavaScript

}

How do I convert String to Money?

Advertisement

Answer

One solution would be to create custom deserilizer for Money field, and then use Jackson to set it.

Here is code snippet:

MoneyDeserializer.java :

JavaScript

Governor.java

JavaScript

NOTE: On your form, you are passing age field as Double, but in your class it’s declared as Integer. So you will get exception during deserilization process. Same thing applys for populationBelowPoverty field . Also, your date format is dd/MM/YYYY and I think this is not supported format for jackson. It should be YYYY-MM-dd.

Anyway, for example, if you send a JSON like this:

JavaScript

to this controller method:

JavaScript

You should get response like this :

JavaScript

In your case, since you are using thymeleaf you can adjust this idea to your needs.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement