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. MySQL Disk--SSD与RAID

    ===================================================SSD与RAID 51.在RAID 5这类Parity-RAID上存在partial-stripe ...

  2. [Python] 中文路径和中文文本文件乱码问题

    情景: Python首先读取名为log.txt的文本文件, 其中包含有文件名相对路径信息filename. 随后Python调用shutil.copy2(src, dst)对该filename文件进行 ...

  3. Oracle活动会话历史(ASH)及报告解读

    对于数据库运行期间的各种状态的实时监控以及相关性能数据捕获对于解决性能问题,提高整体业务系统运行效率是至关重要的.在Oracle数据库中,实时捕获相关性能数据是通过ASH工具来实现的.ASH通过每秒钟 ...

  4. 什么是Base64算法?什么情况下用Base64算法?

    base64 编码的本质:将 8bit 二进制数转化为 6bit 的可打印字符. Base64编码用于需要将二进制数据转为文本数据进行储存和传输的场景. Javascript内部的字符串,都以utf- ...

  5. 查看Linq to Sql生成的sql语句(转)

    查看Linq to Sql生成的sql语句   在控制台项目中,比较简单,直接db.Log = Console.Out;就OK了 但是在其他项目中,需要处理如下: StreamWriter sw = ...

  6. 【python】if&&for&&while语句

    if语法:  类型一: if expression : if_suit else: else_suit 例如: adic={"name":"paulwinflo" ...

  7. 【appium】根据UIAutomator定位元素

    text属性的方法 driver.find_element_by_android_uiautomator('new UiSelector().text("Custom View") ...

  8. debian下配置keepalived ha

    抄袭自http://blog.51yip.com/server/1417.html,做了一些修改 可以参考http://blog.linuxphp.org/archives/1615/ 备注:NAT模 ...

  9. [转]常用字符与ASCII代码对照表

    常用字符与ASCII代码对照表 为了便于查询,以下列出ASCII码表:第128-255号为扩展字符(不常用) ASCII码 键盘 ASCII 码 键盘 ASCII 码 键盘 ASCII 码 键盘 27 ...

  10. latex 入门及使用

    latex 入门及使用 LaTeX新人教程,30分钟从完全陌生到基本入门 >> vim test.tex \documentclass[11pt,twoside,a4paper]{arti ...