Java 来自 json 的 Spring mvc RequestMapping

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20400233/
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-13 01:40:09  来源:igfitidea点击:

Spring mvc RequestMapping from json

javaspring-mvc

提问by user2862639

I'm having trouble creating the appropriate RequestParams for the following Json String:

我在为以下 Json 字符串创建适当的 RequestParams 时遇到问题:

{
  "input": [
    {
      "personAdres": {
        "plaats": "Amsterdam",
        "straat": "Grietenstraat",
        "huisnummer": "12",
        "postcode": "4512UN""
      },
      "interesses": [
        "gas_station",
        "soccer"
      ]
    },
    {
      "personAdres": {
        "plaats": "Arnhem",
        "straat": "Koningsweg",
        "huisnummer": "3",
        "postcode": "1953AA"
      },
      "interesses": [
        "gas_station",
        "soccer"
      ]
    }
  ]
}

I tried the following:

我尝试了以下方法:

 @RequestMapping(method = RequestMethod.GET, params = {"input", "personAdres", "plaats", "straat", "huisnummer", "postcode", "interesses"})
    public
    @ResponseBody`enter code here`
    String getMovie(
            @RequestParam(value = "input") String[] input,
            @RequestParam(value = "personAdres") String[] personAdres,
            @RequestParam(value = "plaats") String plaats,
            @RequestParam(value = "straat") String straat,
            @RequestParam(value = "huisnummer") String huisnummer,
            @RequestParam(value = "postcode") String postcode,
            @RequestParam(value = "interesses")String[] interesses,
            ModelMap model
    )

That doens't seem to work. I'm getting the following error.

那似乎行不通。我收到以下错误。

No matching handler method found for servlet request:

找不到与 servlet 请求匹配的处理程序方法:

Can anyone help me out to create the right requestParams.

任何人都可以帮助我创建正确的 requestParams。

Edit: this seems to work

编辑:这似乎有效

@Controller
@RequestMapping("/dateSuggestie")
public class DateController {

    @RequestMapping(method = RequestMethod.GET)
    public
    @ResponseBody
    String getMovie(
            @RequestParam(value = "input[0][personAdres][plaats]") String p0Plaats,
            @RequestParam(value = "input[0][personAdres][straat]") String p0Straat,
            @RequestParam(value = "input[0][personAdres][huisnummer]") String p0HuisNummer,
            @RequestParam(value = "input[0][personAdres][postcode]") String p0PostCode,
            @RequestParam(value = "input[0][interesses][]") String[] p0Interesses,
            @RequestParam(value = "input[1][personAdres][plaats]") String p1Plaats,
            @RequestParam(value = "input[1][personAdres][straat]") String p1Straat,
            @RequestParam(value = "input[1][personAdres][huisnummer]") String p1HuisNummer,
            @RequestParam(value = "input[1][personAdres][postcode]") String p1PostCode,
            @RequestParam(value = "input[1][interesses][]") String[] p1Interesses) {

采纳答案by M. Deinum

You are sending JSON to your controller not request parameters. @RequestParamand @ModelAttributeonly work when data is submitted as request parameters.

您将 JSON 发送到您的控制器而不是请求参数。@RequestParam并且@ModelAttribute仅在数据作为请求参数提交时才有效。

Your JSON is send to the controller as the request body. For this spring has the @RequestBodyannotation. In general you don't want to parse the body yourself but use a framework to do the heavy lifting for you. For this purpose libraries like Hymansonexist.

您的 JSON 作为请求正文发送到控制器。对于这个春天有@RequestBody注解。一般来说,您不想自己解析身体,而是使用框架来为您完成繁重的工作。为此目的,存在像Hymanson这样的库。

These frameworks also integrate with Spring as one can read in the reference guide.

这些框架还与 Spring 集成,可以在参考指南中阅读。

You need to construct an object which is a Java representation of your JSON so that Hymanson can do the conversion. You can then rewrite your controller method to something like this

您需要构造一个对象,它是您的 JSON 的 Java 表示,以便 Hymanson 可以进行转换。然后,您可以将控制器方法重写为这样的

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String getMovie(@RequestBody YourObject) { ... }

回答by Ernestas Kardzys

Personally I love Hymanson library - it helps mapping JSON object to/from Java's POJO.

我个人喜欢 Hymanson 库 - 它有助于将 JSON 对象映射到/从 Java 的 POJO。

Take a look at MKYONG's tutorial: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/(download ZIP with project). Or take a look at this thread: Parsing JSON in Spring MVC using Hymanson JSON

看看 MKYONG 的教程:http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/ (下载带有项目的 ZIP)。或者看看这个线程:Parsing JSON in Spring MVC using Hymanson JSON