SpringMVC中@ResponseBody和@RequestBody的使用

- @ResponseBody:该注解用于将Controller的方法返回的对象,通过HttpMessageConverter接口转换为指定格式的数据如:json,xml等,通过Response响应给客户端

- @RequestBody:用于获取请求体内容。直接使用得到是key=value&key=value...结构的数据。 get请求方式不适用。

下面为一个简单的演示:

pom.xml

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>        
    
    <!--Springmvc默认用MappingJacksonHttpMessageConverter对json数据进行转换,
        需要加入jackson的包 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.9.0</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.0</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.9.0</version>
            </dependency>

SpringMVC.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置创建spring容器要扫描的包 -->
    
        <!-- 默认使用基于注释的适配器和映射器 -->
        <mvc:annotation-driven"/>
    
        <context:component-scan base-package="com.itheima"></context:component-scan>
    
        <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
                                                
        <!--在springmvc的配置文件中可以配置,静态资源不过滤: -->
        <!-- location表示路径,mapping表示文件,**表示该目录下的文件以及子目录的文件 -->
        <mvc:resources location="/scripts/" mapping="/scripts/**"/>
    </beans>

UserController.java

        /**
         * 获取请求参数
         * 表单发送请求时,请求正文(参数)体现形式是:key=value
         * 此时springmvc为我们封装了数据,但是我们得到的只有表单提交数据的值。
         * 只有请求正文是key=value的情况下,springmvc才能实现为我们封装
         * 而json格式数据,它不是key=value的方式,而是
         *  {key:value}的方式,所以springmvc不会为我们封装。
         * @return
         */
    
    @Controller
    public class UserController {
      
          /**
         * 接收异步请求
         * @RequestBody的作用:
         *      它是获取全部的请求正文内容,包括了请求参数的名称和值
         *
         *    表单参数是key=value时,它的MIME类型是 application/x-www-form-urlencoded
         *    当是json格式时,它的MIME类型是application/json
         *
         *    获取得到的:
         *      [{"username":"test","age":22},{"username":"test","age":22}]
         *
         *  jackson开源组件:
         *      它是借助RequestBody得到的全部请求参数,实现绑定到实体对象中
         *
         *
         *   @ResponseBody
         *         作用:它是用于把控制器方法的返回值转成json并响应给浏览器
         *
         * @return
         */
        @ResponseBody
        @RequestMapping("testJson")
        public User testJson(@RequestBody User user){
            System.out.println(user);
            return user;
        }
    }

testJson.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <!-- 在webapp/scripts目录下导入jquery-3.3.1.min.js -->
    <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-3.3.1.min.js"></script>
    <script type="text/javascript"> $(function () {
        $("#testJson").click(function () {
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/testJson",
                contentType: "application/json;charset=utf-8",
                data: '{"username":"zhangsan","password":"123456","age":18}',
                dataType: "json",
                success: function (data) {
                    alert(data);
                    alert(data.username);
                }
            });
        });
    }) </script>
    <!-- 测试异步请求 -->
    <input type="button" value="测试ajax请求json和响应json" id="testJson"/>
    </body>
    </html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值