首页 技术 正文
技术 2022年11月14日
0 收藏 814 点赞 4,470 浏览 2249 个字

@ResponseBody用法:

作用:该注解用于将Controller的方法返回的对象,根据HTTP Request Header的Accept的内容,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

返回的数据不是html标签的页面,而是其他某种格式的数据时(如json,xml等)使用。

配置返回json和xml数据

添加jackson依赖

<dependence>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependence>
<dependence>
<groupId>com.fasterxml.jackson.core</groupId>
<artifacted>jackson-databind</srtifacted>
<version>2.8.1</version>
</dependence>

开启<mvc:annotation-driven>

java代码:

@RequestMapping("/testResponseBody")
public @ResponseBody Person testResponseBody() {
Person p = new Person();
p.setName("xiaohong");
p.setAge(12);
return p;
}

Person类:

@XmlRootElement(name="Person")
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

Ajax代码:

$.ajax({
url:"testResponseBody",
type:'Get',
header: {
Accept:"application/xml" ,
},
success:function(data,testStatus){
console.log(data);
alert(data)
},
error:function(data,textStatus,errorThrown) {
console.log(data);
}
});

分析:

如果没有配置Person类的xml注解,那么只会json数据,无论Accept是什么。

如果配置了Person类的xml注解,那么如果Accept含有application/xml,就会返回xml数据,例如通过浏览器直接访问,浏览器的http request header appect字段一般都为Accept:text/html application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8,故返回XML数据。

改accept:“application/json”,即可返回json数据

用此注解或者ResponseEntity等类似类,会导致response header 含有accept-charset这个字段,而这个字段对于响应头是没有用的,以下方法可以关掉。

<mvc:annotation-driven>
<mvc:async-support default-timeout="3000/">
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8">
<property name="writeAcceptCharset" value="false">
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

@RequestBody使用:

作用:注解用于将controller的方法参数,根据HTTP Request Header的content-Type的内容,通过适当的HttpMessageConverter转换为java类

使用时机:

POST或者PUT的数据是JSON格式或者是JSON格式或者是XML格式,而不是普通的键值对形式

如何使用:

配置controller

@RequestMapping(value=“/testRequestBody”,method=RequestMethod.POST)
@ResponseBody
public Person testRequestBody(@RequestBody Person p) {
System.out.println("creating a employee:" + p);
return p;
}

Ajax代码如下:

$.ajax({
url:"testResponseBody",
data:{"name":"小红","age":"123"}, // json形式要用双引号
content
});

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849