Spring MVC JSON (JSON to Java)

Logo_Spring_258x151Using Spring MVC we can send to the client data in JSON format, but what if the client sends POST request with data in JSON format? Here we will see how Spring MVC “auto” maps JSON data sent by client POST request into Java object.


  • How client request “GET” data in json format?
  • How client send “POST” data in json format using jQuery.ajax()?
  • How Spring MVC Controller can map json into java object?


  • Eclipse
  • Maven (optional)
  • Browser (Firefox, Chrome or IE)
  • Java Server  (Jetty, Apache,…)


  • Spring Framework
  • Jackson
  • jQuery
        <dependency><!-- spring mvc -->
        <dependency><!-- jackson -->

( 1 ) Java Project

( 2 ) Java Code & XML Configuration

  • RestController.java
package com.hmkcode.controllers;

import javax.servlet.http.HttpServletResponse;

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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hmkcode.vo.Person;

public class RestController {

  public RestController(){
      System.out.println("init RestController");

  //this method responses to GET request http://localhost/spring-mvc-json/rest/cont
  // return Person object as json

  @RequestMapping(method = RequestMethod.GET)
  public @ResponseBody Person get(HttpServletResponse res) {

      Person person = new Person();

      return person;

  //this method response to POST request http://localhost/spring-mvc-json/rest/cont/person
  // receives json data sent by client --> map it to Person object
  // return Person object as json
  @RequestMapping(value="person", method = RequestMethod.POST)
  public @ResponseBody Person post( @RequestBody final  Person person) {    

      System.out.println(person.getId() + " " + person.getName());
      return person;
  • Person.java
package com.hmkcode.vo;

import java.io.Serializable;

public class Person implements Serializable {

  private static final long serialVersionUID = 1L;
  private int id;
  private String name;

//getters and setters...

  • rest-servlet.xml
<beans ...>

  <context:component-scan base-package="com.hmkcode.controllers" />
  <mvc:annotation-driven />

  • web.xml

  • index.html –> $.ajax()
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" >

function sendAjax() {

    url: "/spring-mvc-json/rest/cont/person", 
    type: 'POST', 
    dataType: 'json', 
    data: "{\"name\":\"hmkcode\",\"id\":2}", 
    contentType: 'application/json',
    mimeType: 'application/json',
    success: function(data) { 
        alert(data.id + " " + data.name);
    error:function(data,status,er) { 
        alert("error: "+data+" status: "+status+" er:"+er);

( 3 ) Run & Test

  • Run Jetty “or your” server
  • http://localhost:8080/spring-mvc-json/rest/cont
  • Thsi url will call the first method RestController.get() return Person object in json format

  • http://localhost:8080/spring-mvc-json/index.html
  • This html page contains jQuery.ajax() function that will call the second method RestController.post() on the load of the paeg.
  • $.ajax() will send json data data: “{\”name\”:\”hmkcode\”,\”id\”:2}”
  • Controller will receive this json data mapp into java object using @RequestBody
  • Controller will return data again to the client in json format too.

Soruce Code: @ github

Related Spring MVC post on Spring Framework Tutorial

One thought on “Spring MVC JSON (JSON to Java)

Comments are closed.