开发者

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开发者_JAVA百科 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?


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.


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜