Spring MVC -> JSON response

纵然是瞬间 提交于 2019-11-26 22:52:04

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/4203333/spring-mvc-json-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!