@ResponseBody注解使用简介

本文详细解析了SpringMVC框架中@ResponseBody注解的功能和使用方法,包括其如何将返回值转换为JSON格式,处理中文编码问题,以及与@RequestMapping注解的配合使用。同时,介绍了使用该注解所需的json相关包及其版本匹配的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、@ResponseBody注解简介:

  • @ResponseBody注解
    • 主要做两件事:1、将返回值转换成JSON,如果返回值是String或者其他基本数据类型则不满足key-value形式,不能转换成json类型,则返回字符串

    • 2、设置响应头为application/json;charset=utf-8;返回值为字符串,则不能转换成json格式的则响应头设置为text/html,

    • 为防止中文乱码,因此需要使用@RequestMapping(,produces = “text/html;charset=utf-8”)设置响应头编码方式为utf-8

    • 如果方法只是用注解@RequestMapping()注解,则只要有返回值,无论返回值是什么,都会执行跳转操作;返回值设置成void则不执行跳转。

    • 在使用@RequestMapping()注解的前提下,使用@ResponseBody()注解,则有返回值会使用@ResponseBody()注解进行转换并返回前台页面,不会执行跳转操作。

    • 使用注解@RequestMapping()需要导入(json相关)包:jackson-annotations-xxx.jar、jackson-core-xxx.jar、jackson-databind-xxx.jar

    • 还要注意spring版本和json包版本对应,此处使用spring-4.1.6和jackson-2.4.0版本

2、引入的包:
在这里插入图片描述
3、sprigMVC环境搭建在上一篇博客《Spring MVC请求参数传值、重定向(redirect)与转发(forward)》
介绍。

4、@ResponseBody在Controller类中的使用
4.1、返回值会转成json数据

@RequestMapping("Demo11")
    @ResponseBody
    public People Demo11(People peo){
        System.out.println("这是Demo11!!!!"+peo.getName());
        People p = new People();
        p.setAge(peo.getAge());
        p.setName(peo.getName());
        System.out.println("@@@@@@@@@@@@22 -- "+p.toString());
        return p;
    }

返回结果(在页面中展示):
在这里插入图片描述
4.2、返回值为中文字符串,需要设置字符编码:
返回值为字符串的情况:返回值为字符串,则不能转换成json格式的,返回的响应头 (conten-type) 为text/html,
为防止中文乱码,因此需要使用@RequestMapping(,produces = “text/html;charset=utf-8”)设置响应头编码方式为utf-8

实例代码:

@RequestMapping(value = "Demo12",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String Demo12(People peo){
        return peo.getName();
    }

返回值显示结果:
在这里插入图片描述
5、前端jsp页面:
demo.jsp:

<%@page contentType="text/html; utf-8" pageEncoding="UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<hr>

<form action="Demo12" method="post">
    <table>
        <tr>
            <td>用户姓名:</td>
            <td><input type="text" name="name"/></td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td>
                <input type="number" name="age"/>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交"/>
                <input type="reset" value="重置"/>
            </td>
        </tr>
    </table>
</form>
</body>
</html>

6、使用@ResponseBody特别注意的是要引入的json相关的包
在这里插入图片描述

要注意jackson包的版本要与Spring包的版本相匹配,不然会报错:

[org.springframework.context.support.ClassPathXmlApplicationContext]Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter’: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge

此处使用的版本是Spring-4.1.6-xxx.jar 和 jackson-2.4.0-xxx.jar版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值