Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between DataBinder and ConversionService in Spring

I am experiencing some confusion in the use and purpose of Spring's DataBinder and ConversionService with regards to binding web requests to model objects. This has arisen because I have recently tried to use the JSR-303 validation by adding .

Prior to this I used:

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="mypackage.GlobalWebBindingInitializer" />
    </property>
</bean>

This was good because I wanted a global DataBinder that could be used by several Controllers. Within the GlobalWebBindingInitialzer class implement several of these:

binder.registerCustomEditor(MyClass.class, new PropertyEditorSupport(MyClass.class)

However I wanted to use the @Valid annotation and so added . The side-effect of this is that the above AnnotationMethodHandlerAdapter bean is already defined as part of the annotation-driven and so my global data binder is ignored.

So now I have created this class:

public class MyClassConverter implements Converter<String, MyClass>

I am confused. If I want to use should I use conversion service rather than databinder?

like image 276
jtsnr Avatar asked Feb 07 '12 09:02

jtsnr


People also ask

What is Spring DataBinder?

Data binding is useful for allowing user input to be dynamically bound to the domain model of an application (or whatever objects you use to process user input). Spring provides the so-called DataBinder to do exactly that.

Does Spring MVC has no Customisable data binding?

Spring MVC provides one more annotation, @ModelAttributes , for binding data to the Command object. It is another way to bind the data and to customize the data binding. This annotation allows you to control the creation of the Command object.

What is Spring InitBinder?

Annotation Type InitBinderAnnotation that identifies methods that initialize the WebDataBinder which will be used for populating command and form object arguments of annotated handler methods.

Which Spring template class binds that data to custom domain types?

To help you with that task, Spring provides a convenient template class called RestTemplate . RestTemplate makes interacting with most RESTful services a one-line incantation. And it can even bind that data to custom domain types.


1 Answers

Historically Spring's data binding was used to convert data into javabeans. It relies heavily on JavaBean PropertyEditors to do the conversion.

Spring 3.0 added new and different support for conversions and formatting. Some of the changes included a "core.convert" package and a "format" package that as per the docs "may be used as simpler alternatives to PropertyEditors."

Now, to answer your question, yes, it looks like you're on the right track. You can continue to use either, but to make a long story short in many cases you should be able to use a converter instead of a data binder.

Documentation about how to add validation is available online.

like image 186
jtoberon Avatar answered Sep 27 '22 15:09

jtoberon