Programmer
Programmer

Reputation: 39

Convert string with commas to double number in spring MVC

i am currently using MappingJacksonHttpMessageConverter in my Dispatcher-Servlet.xml. In my application when the user enters a currency, commas are itself attached to the value. When that value is passed to the controller it is unable to convert the string with commas to a double value. throws pare error. I am using this value for other calculations so i cannot change its type to string in the modal class.

Is there a way we can can use MappingJacksonHttpMessageConverter to make the controller ignore the commas and read the value.

This is part of my servlet.xml file.

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="order" value="1" />
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
        </list>
    </property>
</bean>

Any help would be appreciated

Upvotes: 1

Views: 1691

Answers (1)

varun
varun

Reputation: 726

You can do this by creating custom conversion service in spring by implementing Converter interface. For info on this see http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html and http://log4aj.blogspot.in/2012/08/spring-converterconversionservice.html.

Upvotes: 1

Related Questions