Tim
Tim

Reputation: 13258

Spring MVC -> JSON response

I hava a JAVA EE backend and I am using Spring MVC. I have a AJAX call like this:

function getAllProjects() {
        $.getJSON("project/getall", function(allProjects) {
            ???
        });
    }

My backend system:

@RequestMapping(value="/getall", method=RequestMethod.GET)
public @ResponseBody ??? getAllProjects() {
    ???
}

What is the content I have to implement so it will work? In the backend system I have from a database call the unique id and the name of the project, for example:

1 => My Test Project
4 => Another One
23 => One More Test

The id and the project name should be returned to the frontend system, so I can build a HTML ul/li list in this kind:

<ul>
    <li><a href="/1">My Test Project</a></li>
    <li><a href="/4">Another One</a></li>
    <li><a href="/23">One More Test</a></li>
</ul>

Does anyone know how this can be done?

Upvotes: 13

Views: 24124

Answers (5)

John Culviner
John Culviner

Reputation: 22944

The rest of these answers are extremely out-of-date! It's very easy now

  • add Jackson2 to your classpath
  • use @RestController

ex:

@RestController
public class MyController {

    @RequestMapping("/thing")
    public MyThing thing() {
        return new MyThing();
    }

}

ref: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-write-a-json-rest-service

Upvotes: 2

severedsea
severedsea

Reputation: 174

You can also use org.json's JSONArray and JSONObject to construct the JSON output, then, return a String value as the @ResponseBody.

http://www.json.org/javadoc/org/json/JSONObject.html

@RequestMapping(value="/getall", method=RequestMethod.GET)
public @ResponseBody String getAllProjects() {
    ...
    JSONArray jsonItems = new JSONArray();

    JSONObject jsonItem1 = new JSONObject();
    jsonItem1.put("id", "1");
    jsonItem1.put("name", "My Test Project");

    JSONObject jsonItem2 = new JSONObject();
    jsonItem2.put("id", "4");
    jsonItem2.put("name", "Another one");

    jsonItems.put(jsonItem1);
    jsonItems.put(jsonItem2);

    return jsonItems.toString();
}

You should get something like this in your ajax request's success callback.

[{
   "id":"1",
   "name":"My Test Project"
},{
   "id":"4",
   "name":"Another one"
}]

You can use this data to either append your ul li using javascript or using _underscore templating to render your UI.

Upvotes: 8

MatBanik
MatBanik

Reputation: 26860

As suggested here: Spring 3 JSON with MVC checkout this website: http://spring-json.sourceforge.net/ It has perfectly nice working example on how to do this in spring framework.

Upvotes: 1

axtavt
axtavt

Reputation: 242686

You need to:

  • Add Jackson JSON Mapper to the classpath
  • Add <mvc:annotation-driven> to your config
  • Return Map<Integer, String>

For more complex cases when you need to configure mapping process for each handler method you may use MappingJacksonJsonView instead of @ResponseBody, as Stepen C suggested.

Upvotes: 14

Stephen C
Stephen C

Reputation: 718698

You need to read Chapter 15.5 of the Spring User Guide which describes how to configure MVC views, and Chapter 16.10 which briefly describes the JSON Mapping View. Then read the javadocs for MappingJacksonJsonView etc.

Upvotes: 8

Related Questions