I have inherited a spring MVC project that basically has several controllers. I came across a declaration in an xml file. Apparently, this declaration allows for writing rest-based services and clients. Could someone explain with an example the apparent magic these declarations allow for?
<bean id="restClient" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<util:list>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
<bean id="formMessageConverter" class="org.springframework.http.converter.FormHttpMessageConverter"/>
</util:list>
</property>
</bean>
Thank you in advance.
resttemplate by spring , provided to convert your java object to desired output(html string, xml, json etc) during the service call over the network, and inturn the received response from the service will be unmarshalled back to java object or desired datatype.
through above configuration resttemplate will use those appropriate converters to handle different datatypes, as i said it may be html, json or application xml
all we are doing is, with out writing java code , we are configuring the resttemplate and will be in spring context , this configuration applies where ever your resttemplate been used.
i have example here
let us say we invoke a service to check an user is valid user or not
and service response with some code as 1 is valid and 0 as invalid
first you need to marshall into any of the acceptable datatype , let us have application/xml
all i am doing here is through config file
to above configuration i am adding jaxb marshaller and unmarshaller (see above config)
i have configured both marshaller and unmarshaller, and i am telling the acceptable datatypes which both should use while marshalling and unmarshalling
and finally, the below configuration tells the java objects which are acceptable during marshalling (request.User will be converted to xml) and unmarshalling (xml to convert back to response.validUser)
on here comes the java code
here you directly pass your java object , and your resttemplate will marshall it without any hassle !!!
like wise you can also handle plain html text
if you see, the above java code, dont have any message converter code, marshaller and unmarshaller logic, all done in one liner with use of spring configuration.