Spring 和 Jackson 的新手 2. 这个 bean 声明在 Spring MVC 应用程序中允许什么? [英] New to Spring and Jackson 2. What does this bean declaration allow for in a Spring MVC application?

查看:68
本文介绍了Spring 和 Jackson 的新手 2. 这个 bean 声明在 Spring MVC 应用程序中允许什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个spring MVC项目,基本上有几个控制器.我在 xml 文件中遇到了一个声明.显然,这个声明允许编写基于休息的服务和客户端.有人可以举例说明这些声明所允许的明显魔法吗?

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>

提前致谢.

推荐答案

resttemplate by spring ,用于在通过网络的服务调用期间将您的 java 对象转换为所需的输出(html 字符串、xml、json 等),并将从服务收到的响应将被解组回 java 对象或所需的数据类型.

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>

通过上面的配置,resttemplate 将使用那些合适的转换器来处理不同的数据类型,正如我所说的可能是 html、json 或应用程序 xml

through above configuration resttemplate will use those appropriate converters to handle different datatypes, as i said it may be html, json or application xml

我们所做的只是,无需编写 java 代码,我们正在配置 resttemplate 并将在 spring 上下文中,此配置适用于您使用 resttemplate 的任何地方.

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.

我这里有例子

假设我们调用一个服务来检查用户是否为有效用户

let us say we invoke a service to check an user is valid user or not

class User{
   string userId;

   string password;

}

和一些代码为 1 的服务响应有效,0 为无效

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

class ValidUser{
   int validCode;
}

首先你需要编组成任何可接受的数据类型,让我们有 application/xml

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

在上面的配置中,我添加了 jaxb marshaller 和 unmarshaller(见上面的配置)

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

最后,下面的配置告诉 java 对象在编组(request.User 将转换为 xml)和解组(xml 将转换回 response.validUser)期间可接受的对象

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>

Java 代码来了

在这里你直接传递你的java对象,你的resttemplate会毫不费力地编组它!!!

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();

同样,你也可以处理纯 html 文本

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);

如果你看到上面的java代码,没有任何消息转换器代码、编组器和解组器逻辑,所有这些都在一个使用spring配置的衬垫中完成.

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.

这篇关于Spring 和 Jackson 的新手 2. 这个 bean 声明在 Spring MVC 应用程序中允许什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆