New to Spring and Jackson 2. What does this bean d

2019-08-21 03:41发布

问题:

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.

回答1:

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.

<property name="messageConverters">
    <util:list>
        <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        <bean id="formMessageConverter" class="org.springframework.http.converter.FormHttpMessageConverter"/>
        <bean id="messageConverter"
                    class="org.springframework.http.converter.StringHttpMessageConverter" />

    </util:list>

  //marhsaller and unmarshaller

    <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <property name="marshaller" ref="jaxbMarshaller"></property>
    <property name="unmarshaller" ref="jaxbMarshaller"></property>
    <property name="supportedMediaTypes">
    <list>
       <value>application/xml</value>
       <value>text/xml</value>
       <value>json</value>
     </list>
</property>

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

class User{
   string userId;

   string password;

}

and service response with some code as 1 is valid and 0 as invalid

class ValidUser{
   int validCode;
}

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)

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.request.user</value>
            <value>com.response.validUser</value>

        </list>
    </property>

</bean>

on here comes the java code

here you directly pass your java object , and your resttemplate will marshall it without any hassle !!!

        User user = new User();
        user.setName('test');
        user.setPassword('password');
        // setting media type as xml, and telling convert my user java obj to xml
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);
        HttpEntity<User> entity = new   HttpEntity<User>   (user , headers);

        ResponseEntity<validUser> responseEntity = rest.exchange(URL, HttpMethod.POST, entity, validUser.class);
    // let us assume that service response for valid user <validCode>1<validCode>
    //then validuserreponse obj will have code as 1, let us say valid user.
    ValidUser validUserResponse = responseEntity.getBody();

like wise you can also handle plain html text

headers.setContentType(MediaType.TEXT_HTML);

    HttpEntity<String> entity = new HttpEntity<String>(htmlString, headers);
    ResponseEntity<String> htmlStringresponse = rest.postForEntity(URL, entity, String.class);

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.