springmvc的@ResponseBody注解的作用。

来源:互联网 发布:ppt数据分析 编辑:程序博客网 时间:2024/06/05 06:11

转自:http://zhidao.baidu.com/link?url=KHPLyXqL5dGzd4oFEiw0wRe_XZBu6SU0NjLNlIInkwbckBAyjJTYZwOQ1Hk9nnyEt7_mi0cgvTllUczTtiWtfK


GET模式下,这里使用了@PathVariable绑定输入参数,非常适合Restful风格。因为隐藏了参数与路径的关系,可以提升网站的安全性,静态化页面,降低恶意攻击风险。
POST模式下,使用@RequestBody绑定请求对象,Spring会帮你进行协议转换,将Json、Xml协议转换成你需要的对象。
@ResponseBody可以标注任何对象,由Srping完成对象——协议的转换。


注意:spring-mvc.xml的相关配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">


<!-- 自动扫描 com.jikexueyuan 包下面的所有组件(使用了springmvc注解)-->
<context:component-scan base-package="com.jikexueyuan.*" />
    
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <util:list id="beanList">
            <!-- 配置处理JSON数据 -->
                <ref bean="mappingJacksonHttpMessageConverter" />
            </util:list>
        </property>
    </bean>
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>      
</beans>

0 0
原创粉丝点击