Posting JSON to Spring MVC Controller
Spring MVC can be setup to automatically bind incoming JSON string into a Java object. Firstly, ensure you have jackson-mapper-asl included on the classpath:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
Assuming the JSON string we want to bind represent a person object like this:
{
name: "Gerry",
age: 20,
city: "Sydney"
}
Which will be bound into following Java class:
public class Person {
private String name;
private int age;
private String city;
// getters & setters ...
}
Declare the Spring MVC controller handler method like below. The handler method is mapped into path “addPerson” with method POST.
@RequestMapping(value = "/addPerson",
method = RequestMethod.POST,
headers = {"Content-type=application/json"})
@ResponseBody
public JsonResponse addPerson(@RequestBody Person person) {
logger.debug(person.toString());
return new JsonResponse("OK","");
}
Also pay attention to @ResponseBody and the returned JsonResponse object. Here JsonResponse is a class to return the result of the addPerson operation.
public class JsonResponse {
private String status = "";
private String errorMessage = "";
public JsonResponse(String status, String errorMessage) {
this.status = status;
this.errorMessage = errorMessage;
}
}
When converted to JSON this will look like this:
{
status : "OK",
errorMessage : ""
}
Below is an example jQuery based javascript handler that posts into the above Spring MVC handler and observe the returned value:
$.ajax({
type: "POST",
url: "addPerson",
data: JSON.stringify({ name: "Gerry", age: 20, city: "Sydney" }),
contentType: 'application/json',
success: function(data) {
if(data.status == 'OK') alert('Person has been added');
else alert('Failed adding person: ' + data.status + ', ' + data.errorMessage);
}
});
Posting JSON to Spring MVC Controller的更多相关文章
- Spring MVC Controller与jquery ajax请求处理json
在用 spring mvc 写应用的时候发现jquery传递的[json数组对象]参数后台接收不到,多订单的处理,ajax请求: "}]}]} $.ajax({ url : url, typ ...
- 前台JSON字符串,spring mvc controller也接收字符串
前台JSON字符串,spring mvc controller也接收字符串 前台: $.post(url, { data : JSON.stringify(obj) }, function(data) ...
- Spring MVC Controller中解析GET方式的中文参数会乱码的问题(tomcat如何解码)
Spring MVC Controller中解析GET方式的中文参数会乱码的问题 问题描述 在工作上使用突然出现从get获取中文参数乱码(新装机器,tomcat重新下载和配置),查了半天终于找到解决办 ...
- 转:【Spring MVC Controller单例陷阱】
http://lavasoft.blog.51cto.com/62575/1394669/ Spring MVC Controller默认是单例的: 单例的原因有二:1.为了性能.2.不需要多例. 1 ...
- Spring MVC Controller单例陷阱
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lavasoft.blog.51cto.com/62575/1394669 Spr ...
- Spring MVC Controller中GET方式传过来的中文参数会乱码的问题
Spring MVC controller 这样写法通常意味着访问该请求,GET和POST请求都行,可是经常会遇到,如果碰到参数是中文的,post请求可以,get请求过来就是乱码.如果强行对参数进行了 ...
- Spring MVC Controller 单元测试
简介 Controller层的单元测试可以使得应用的可靠性得到提升,虽然这使得开发的时间有所增加,有得必失,这里我认为得到的比失去的多很多. Sping MVC3.2版本之后的单元测试方法有所变化,随 ...
- spring mvc controller中获取request head内容
spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...
- 关于Spring MVC Controller 层的单元测试
关于Spring MVC Controller 层的单元测试 测试准备工作: 1.搭建测试Web环境 2.注入Controller 类 3.编写测试数据 测试数据的文件名一定要与测试类的文件名相同,比 ...
随机推荐
- stenciljs 学习五 事件
组件可以使用Event Emitter装饰器发送数据和事件. Event 定义 参考: import { Event, EventEmitter } from '@stencil/core'; ... ...
- smarty学习——编程知识
smarty 提供了丰富的api 接口可以方便我们进行操作: 1.clear_all_assign清除所有赋值 2.clear_all_cache清除所有缓存 3.clear_assign清除赋值 4 ...
- c#数据库訪问返回值类型为SqlDataReader时使用using时注意的问题
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010512579/article/details/24011761 在封装通用 SQLSERVER ...
- 【柚子木字幕組】【BBC】 Are Our Kids Tough Enough? Chinese School 英國的孩子足夠堅強嗎?中式教學
https://www.youtube.com/watch?v=ypT6c4NZ6jk 最近很火的一个bbc纪录片 讲的是英国学校请几个中国老师到他们那里试行中国教育的故事 作为学习英文的素材很不错
- 【转】每天一个linux命令(34):du 命令
原文网址:http://www.cnblogs.com/peida/archive/2012/12/10/2810755.html Linux du命令也是查看使用空间的,但是与df命令不同的是Lin ...
- mac系统下 Homebrew 使用
brew 又叫 Homebrew,是一款Mac OS平台下的软件包管理工具. brew 常用命令: 命令 作用 brew install [package] 安装包 brew uninstall [p ...
- netty答题
1,介绍一下netty netty封装了Java原生的nio,是一个异步和数据驱动的网络编程框架, 与tcp: netty -> Java Runtime Socket (io.nio.nio2 ...
- 阿里巴巴Java开发手册-命名规约
1. [强制] 代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束.反例: _name / __name / $Object / name_ / name$ / Object$2. ...
- 使用redis防止商品超发
redis不仅仅是单纯的缓存,它还有一些特殊的功能,在一些特殊场景上很好用.redis中key的原子自增incrby和判断key不存在再写入的setnx方法,可以有效的防止超发. 下面使用两个不同的方 ...
- keepalived配置主从备份
keepalived配置主从备份 keepalived是一个用于做双机热备(HA)的软件,常和haproxy联合起来做热备+负载均衡,达到高可用. 运行原理 keepalived通过选举(看服务器 ...