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框架完成大部分的请求与响应啦~

相关推荐
RainbowSea6 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea6 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑10 小时前
Jpa使用union all
java·spring boot·后端
用户37215742613510 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊11 小时前
Java学习第22天 - 云原生与容器化
java
渣哥13 小时前
原来 Java 里线程安全集合有这么多种
java
间彧13 小时前
Spring Boot集成Spring Security完整指南
java
间彧14 小时前
Spring Secutiy基本原理及工作流程
java
Java水解15 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆17 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试