[英]Spring MVC -> JSON response
I hava a JAVA EE backend and I am using Spring MVC. 我有一个JAVA EE后端,我正在使用Spring MVC。 I have a AJAX call like this: 我有一个像这样的AJAX调用:
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: 在后端系统中,我从数据库中调用唯一的id和项目的名称,例如:
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: id和项目名称应返回到前端系统,因此我可以构建这种类型的HTML ul / li列表:
<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: 你需要:
<mvc:annotation-driven>
to your config 将<mvc:annotation-driven>
到您的配置中 Map<Integer, String>
返回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. 对于更复杂的情况,当您需要为每个处理程序方法配置映射过程时,您可以使用MappingJacksonJsonView
而不是@ResponseBody
,正如Stepen C建议的那样。
You can also use org.json's JSONArray and JSONObject to construct the JSON output, then, return a String value as the @ResponseBody. 您还可以使用org.json的JSONArray和JSONObject来构造JSON输出,然后返回一个String值作为@ResponseBody。
http://www.json.org/javadoc/org/json/JSONObject.html 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. 你应该在你的ajax请求的成功回调中得到这样的东西。
[{
"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. 您可以使用此数据使用javascript附加您的文件或使用_underscore模板来呈现您的UI。
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. 您需要阅读Spring用户指南的第15.5章,其中介绍了如何配置MVC视图,以及第16.10章,其中简要介绍了JSON映射视图。 Then read the javadocs for MappingJacksonJsonView
etc. 然后阅读MappingJacksonJsonView
等的javadocs。
The rest of these answers are extremely out-of-date! 其余的答案都已经过时了! It's very easy now 现在很容易
@RestController
使用@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 参考: http : //docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-write-a-json-rest-service
正如这里建议的那样: 带有MVC的Spring 3 JSON结帐这个网站: http : //spring-json.sourceforge.net/它有一个非常好的工作示例,如何在spring框架中执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.