What should I use i if you need a custom converter when using @RequestParam?

I if there is a method signature as follows

public void deposit(@RequestParam("accountId") Integer accountId, @RequestParam("amount") BigDecimal amount) {...} 

And since I have a specific decimal value for the locale that needs to be converted to BigDecimal , is there some kind of annotation that allows me to configure incoming data like @Decimal ("###. ###, ##") or something else???

+4
source share
2 answers

Spring 3 has @NumberFormat annotation:

 public void deposit(@RequestParam("accountId") Integer accountId, @RequestParam("amount") @NumberFormat(pattern = "###.###,##") BigDecimal amount) {...} 

You need <mvc:annotation-driven> to enable it.

See also:

+4
source

More generally, you can register your own converter with ConversionServiceFactoryBean . After that, you will need to add a custom website binding initializer to your adapter’s adapter

Configuration Example:

 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService" ref="conversionService"/> </bean> </property> </bean> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <ref bean="myCustomConverter"/> </list> </property> </bean> 
+2
source

All Articles