SpringMVC-响应数据

一、引子

我们在上一篇文章SpringMVC-组件解析里介绍了SpringMVC框架执行一个请求的过程,并演示了快速使用Controller承接请求。本篇我们将深入介绍SpringMVC执行请求时,如何响应客户端。

二、响应类型

SpringMVC的数据响应方式主要分为两类(1)页面跳转(2)返回数据。其中,对于(1)页面跳转:又包括直接返回字符串与通过返回ModelAndView对象。对于(2)返回数据:又包括返回普通的字符串与返回集合/对象。

三、页面跳转

页面跳转都是前后端不分离的做法,所以这部分我们简单介绍一下。

(1)返回字符串

返回字符串的方式自我们介绍SpringMVC以来便一直是以这种方式来演示的,所以我们在这里就不再重复演示了。

(2)返回ModelAndView

我们先来做一个简单的回顾:读者是否还记得在JavaWeb阶段,我们声明一个类,然后继承HttpServlet。在重写doPost或doGet方法时形参为HttpServletRequest req, HttpServletResponse resp。我们提到doPost与doGet方法是由谁来调用呢,参数req与resp又是如何传入方法的呢。如果忘记了可以回顾Servlet执行流程Servlet体系结构两篇文章。答案是由Web容器(如Tomcat)来调用与传参的。

在SpringMVC中也是,我们可以在Controller的方法中传入ModelAndView对象,Model对象,甚至是HttpServletRequest、HttpServletResponse等类型的对象。**SpringMVC都会在调用时为你传入这些对象。**示例如下:

java 复制代码
    @RequestMapping("/model")
    public ModelAndView model(ModelAndView modelAndView) {
        modelAndView.setViewName("success");
        return modelAndView;
    }

又如:

java 复制代码
  @RequestMapping("/http")
    public String model(HttpServletRequest httpServletRequest) {
        System.out.println(httpServletRequest);
        httpServletRequest.setAttribute("username", "zhangsan");
        return "success";
    }
四、回写数据

回写数据需要用@ResponseBody注解修饰类或方法,表明不进行视图跳转,而是需要返回数据。

(1)回写普通字符串
java 复制代码
    @RequestMapping("/return")
    @ResponseBody
    public String returnData() {
        return "return";
    }
(2)回写对象

返回对象数据需要JSON序列化,还需要在处理器适配器中配置数据转换器:

添加依赖

XML 复制代码
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.10</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.databind/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.10</version>
        </dependency>

是否还记得在SpringMVC-组件解析中介绍的处理器适配器(HandlerAdapter),在其中配置一个属性:配置数据转换器。这有点类似于在视图解析器配置视图的前后缀。(请读者回顾SpringMVC-组件解析

XML 复制代码
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
            </list>
        </property>
    </bean>

Controller中方法:

java 复制代码
    @RequestMapping("/user")
    @ResponseBody
    public User returnObject() {
        User user = new User();
        user.setName("zhangsan");
        user.setAge(23);
        return user;
    }
五、总结

至此我们完成了SpringMVC-响应的介绍,请读者继续关注,笔者将在下一篇文章中为大家介绍SpringMVC-请求的介绍。届时将可以利用SpringMVC框架完成大部分的请求与响应啦~

相关推荐
Chester_19994 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
EXI-小洲5 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员5 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导6 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan6 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251496 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
2601_961901706 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
何以解忧,唯有..7 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
大衛說7 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
QQ_21696290967 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端