准备

@RequestBody

作用:

@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容(json数据)转换为java对象并绑定到Controller方法的参数上。

@ResponseBody

作用:

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

需求:通过jsp页面请求json,相应json.

本例中:

@RequestBody注解实现接收http请求的json数据,将json数据转换为java对象进行绑定

@ResponseBody注解实现将Controller方法返回java对象转换为json响应给客户端

实现步骤

1 加入jar包

2 准备一个jsp页面

书写一个jsp页面,用于向服务器提交json数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>json数据交互</title> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(function(){
//alert();
//$.post(); //写回的可以指定是json字符串,不能发送json字符串
var params ='{"id": 1,"name": "测试商品","price": 99.9,"detail": "测试商品描述","pic": "123456.jpg"}';
$.ajax({
url : "${pageContext.request.contextPath }/json.action",
data : params,
contentType : "application/json;charset=UTF-8", //发送数据的格式
type : "post",
dataType : "json", //回调
success : function(data){
alert(data.name);
}
});
});
</script>
</head>
<body>
<h1>这也页面用于json数据交互的练习</h1>
</body>
</html>

3 JsonController编写

package com.test.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.test.springmvc.pojo.Items; @Controller
public class JsonController { /*
* json数据交互
*/
@RequestMapping("/json.action")
public @ResponseBody Items jsonTest(@RequestBody Items items){
System.out.println(items);
return items;
}
}

4 Items类

package com.test.springmvc.pojo;

import java.util.Date;

public class Items {
private Integer id; private String name; private Float price; private String pic; private Date createtime; private String detail; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name == null ? null : name.trim();
} public Float getPrice() {
return price;
} public void setPrice(Float price) {
this.price = price;
} public String getPic() {
return pic;
} public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
} public Date getCreatetime() {
return createtime;
} public void setCreatetime(Date createtime) {
this.createtime = createtime;
} public String getDetail() {
return detail;
} public void setDetail(String detail) {
this.detail = detail == null ? null : detail.trim();
}
}

3 配置json转换器

本例中并不需要配置json转换器。因为我使用的注解驱动开发。

如果不使用注解驱动<mvc:annotation-driven />,就需要给处理器适配器配置json转换器,参考之前学习的自定义参数绑定。

在springmvc.xml配置文件中,给处理器适配器加入json转换器:

<!--处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>

最新文章

  1. PHP面向对象——重写与重载
  2. android之LayoutInflater讲解
  3. 远程桌面Default.rdp 中各个参数的含义
  4. 关于u-boot中的.balignl 16,0xdeadbeef的理解
  5. make no mistake, we are the last line of defense.
  6. 运行所有sdk目录下的示例,查看它们的功能,方便以后查寻
  7. lightoj 1013 dp
  8. break、continue和goto 三者作用介绍
  9. 实验三:gdb跟踪调试内核从start_kernel到init进程启动
  10. Baby Step Gaint Step
  11. C++链表模板类
  12. 【转】WPF自定义控件与样式(13)-自定义窗体Window &amp; 自适应内容大小消息框MessageBox
  13. 再次重温《Right here waiting》
  14. cocos2dx解决中文乱码方法
  15. JS-Promise笔记
  16. Flask-论坛开发-1-基础知识
  17. Java异常、事件、多线程
  18. html 画出矩形,鼠标弹起,矩形消失
  19. django web 笔记
  20. 常见IT英语短语一

热门文章

  1. mybatis mybatis.xml 文件和properties文件结合来进行配置数据源
  2. Java中的多态方法
  3. $.ajax()所有参数详解
  4. MongoDB高级知识-易扩展
  5. “全栈2019”Java第五十三章:向上转型和向下转型详解
  6. CF1059C Sequence Transformation 题解
  7. [ActionScript 3.0] 翻牌效果,运用语法rotationY,PerspectiveProjection
  8. iOS 键盘的监听 调整view的位置
  9. UIScreen和UIWindow
  10. JSON与XML比较