Java Spring MVC -> JSON 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4203333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:30:56  来源:igfitidea点击:

Spring MVC -> JSON response

javahtmlajaxjsonspring-mvc

提问by Tim

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

我有一个 JAVA EE 后端,我正在使用 Spring MVC。我有一个这样的 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?

有谁知道如何做到这一点?

采纳答案by axtavt

You need to:

你需要:

  • Add Hymanson JSON Mapperto the classpath
  • Add <mvc:annotation-driven>to your config
  • Return Map<Integer, String>
  • Hymanson JSON Mapper添加到类路径
  • 添加<mvc:annotation-driven>到您的配置
  • 返回 Map<Integer, String>

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

对于更复杂的情况,当您需要为每个处理程序方法配置映射过程时,您可以使用MappingHymansonJsonView而不是@ResponseBody,正如 Stepen C 建议的那样。

回答by Stephen C

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 MappingHymansonJsonViewetc.

您需要阅读 Spring 用户指南的第 15.5 章,其中描述了如何配置 MVC 视图,以及第 16.10 章,其中简要描述了 JSON 映射视图。然后阅读 javadocsMappingHymansonJsonView等。

回答by Mat B.

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

正如这里所建议的: Spring 3 JSON with MVCcheckout 这个网站:http: //spring-json.sourceforge.net/它有关于如何在 spring 框架中做到这一点的非常好的工作示例。

回答by severedsea

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 模板来附加您的 ul li 来呈现您的 UI。

回答by John Culviner

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

其余的这些答案都非常过时!现在很容易

  • add Hymanson2 to your classpath
  • use @RestController
  • 将 Hymanson2 添加到您的类路径中
  • @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