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的更多相关文章

  1. Spring MVC Controller与jquery ajax请求处理json

    在用 spring mvc 写应用的时候发现jquery传递的[json数组对象]参数后台接收不到,多订单的处理,ajax请求: "}]}]} $.ajax({ url : url, typ ...

  2. 前台JSON字符串,spring mvc controller也接收字符串

    前台JSON字符串,spring mvc controller也接收字符串 前台: $.post(url, { data : JSON.stringify(obj) }, function(data) ...

  3. Spring MVC Controller中解析GET方式的中文参数会乱码的问题(tomcat如何解码)

    Spring MVC Controller中解析GET方式的中文参数会乱码的问题 问题描述 在工作上使用突然出现从get获取中文参数乱码(新装机器,tomcat重新下载和配置),查了半天终于找到解决办 ...

  4. 转:【Spring MVC Controller单例陷阱】

    http://lavasoft.blog.51cto.com/62575/1394669/ Spring MVC Controller默认是单例的: 单例的原因有二:1.为了性能.2.不需要多例. 1 ...

  5. Spring MVC Controller单例陷阱

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lavasoft.blog.51cto.com/62575/1394669 Spr ...

  6. Spring MVC Controller中GET方式传过来的中文参数会乱码的问题

    Spring MVC controller 这样写法通常意味着访问该请求,GET和POST请求都行,可是经常会遇到,如果碰到参数是中文的,post请求可以,get请求过来就是乱码.如果强行对参数进行了 ...

  7. Spring MVC Controller 单元测试

    简介 Controller层的单元测试可以使得应用的可靠性得到提升,虽然这使得开发的时间有所增加,有得必失,这里我认为得到的比失去的多很多. Sping MVC3.2版本之后的单元测试方法有所变化,随 ...

  8. spring mvc controller中获取request head内容

    spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...

  9. 关于Spring MVC Controller 层的单元测试

    关于Spring MVC Controller 层的单元测试 测试准备工作: 1.搭建测试Web环境 2.注入Controller 类 3.编写测试数据 测试数据的文件名一定要与测试类的文件名相同,比 ...

随机推荐

  1. hasura graphql-engine v1.0.0-alpha26 版本新功能

    hasura 发布了graphql-engine v1.0.0-alpha26 版本,有一些破坏的变动,以及方便的新特性 破坏性变动 order_by 从 order_by: id_asc 为 ord ...

  2. smarty学习——缓存

    存被用来保存一个文档的输出从而加速display()或fetch()函数的执行.如果一个函数被加进缓存,那么实际输出的内容将用缓存来代替. 缓存可让事物非常快速的执行,特别是带有长计算时间的模板.一旦 ...

  3. 使用js 文件参数 以及IHttpModule实现服务验证asp.net 版的初步实现

    接上面的文章,上面的主要是进行html 页面自己进行处理.但是对于进行asp.net 的开发者以及其他的就显的不太好了. 我的实现方式是使用IHttpModule 进行对于用户请求的带有参数的js文件 ...

  4. nyoj 密码宝盒

    密码宝盒 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描述 小M得到了一个宝盒,可惜打开这个宝盒需要一组神奇的密码,然而在宝盒的下面 有关于密码的提示信息:密码是一个C进制 ...

  5. RabbitMQ负载均衡方案之LVS

    负载均衡的方案有很多,适合RabbitMQ使用的处理HAProxy之外还有LVS.LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件 ...

  6. JavaScriptSerializer类 对象序列化为JSON,JSON反序列化为对象 。

    JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.说白了就是能够直接将一个C#对象传送到前台页面成为javascript对 ...

  7. BASIC-5_蓝桥杯_查找整数

    示例代码: #include <stdio.h>#include <stdlib.h> int main(void){ int n = 0 , key = 0 , count ...

  8. VMware NAT模式下设置网络

    一.虚拟机NAT模式原理 NAT模式在VMware下又称VMnet8.在这种模式下,宿主机有两块网卡,一块是真实的物理网卡(即NAT device),连接Network:一块是 VMware Netw ...

  9. spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情

    <spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情> <服务网关zu ...

  10. ios ideviceintaller安装

    1.安装brew 打开终端输入: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/inst ...