来自json的Spring mvc RequestMapping [英] Spring mvc RequestMapping from json

查看:110
本文介绍了来自json的Spring mvc RequestMapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

{
  "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"
      ]
    }
  ]
}

我尝试了以下方法:

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

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

No matching handler method found for servlet request:

任何人都可以帮忙我打算创建正确的请求.Params。

Can anyone help me out to create the right requestParams.

编辑:这似乎有用

@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) {


推荐答案

您正在向控制器发送JSON不要求参数。 @RequestParam @ModelAttribute 仅在数据作为请求参数提交时才有效。

You are sending JSON to your controller not request parameters. @RequestParam and @ModelAttribute only work when data is submitted as request parameters.

您的JSON作为请求正文发送给控制器。对于这个春天,有 @RequestBody 注释。一般来说,你不想自己解析身体,而是使用框架为你做繁重的工作。为此,存在像 Jackson 这样的图书馆。

Your JSON is send to the controller as the request body. For this spring has the @RequestBody annotation. 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 Jackson exist.

这些框架也可以与Spring集成,因为人们可以阅读参考指南

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

您需要构建一个对象,它是JSON的Java表示形式,以便Jackson可以执行转换。然后你可以将你的控制器方法重写为这样的东西

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

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

这篇关于来自json的Spring mvc RequestMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆