Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring mvc RequestMapping from json

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

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:

Can anyone help me out to create the right 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) {
like image 398
user2862639 Avatar asked Dec 05 '13 12:12

user2862639


2 Answers

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

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.

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

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) { ... }
like image 67
M. Deinum Avatar answered Sep 30 '22 19:09

M. Deinum


Personally I love Jackson library - it helps mapping JSON object to/from Java's 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 Jackson JSON

like image 31
Ernestas Kardzys Avatar answered Sep 30 '22 19:09

Ernestas Kardzys