Posting JSON to Spring MVC Controller
2016-01-05 10:46
375 查看
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:
Assuming the JSON string we want to bind represent a person object like this:
Which will be bound into following Java class:
Declare the Spring MVC controller handler method like below. The handler method is mapped into path “addPerson” with method POST.
Also pay attention to @ResponseBody and the returned JsonResponse object. Here JsonResponse is a class to return the result of the addPerson operation.
When converted to JSON this will look like this:
Below is an example jQuery based javascript handler that posts into the above Spring MVC handler and observe the returned value:
<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); } });
相关文章推荐
- Spring中bean的生命周期
- Spring + iBATIS完整示例
- Java 单例模式详解
- java synchronized详解
- eclipse jee配置jetty的两种方法
- 协同开发环境搭建(SVN+Mysql+nat123+Myeclipse)
- java程序(一)----HashMap同时获取键值
- JAVA学习【1】书写与命名规则
- SpringMVC框架整合Shiro验证框架
- Myeclipse2014中,新建部署Maven项目
- java反射(转载)
- java线程的run()没有返回值怎么办?
- 深入理解Java国际化
- elasticsearch 常用语法
- 您需要安装旧 Java SE 6 运行环境才能打开“Eclipse”。
- Unable to locate Spring NamespaceHandler for XML schema namespace
- 基础算法8:提取出输入字符串中的域名或IP字符串
- Java Mocking入门—使用Mockito
- java中二进制和流的相互转换
- 1.1 语言与平台 [Java]