1

How can I map multiple beans in my @RestController?

I'm using spring-web-4.3.8.RELEASE.jar

I tried everything: @RequestParam @RequestBody, @RequestAttribute, @RequestPart but nothing works...

package com.example.demo;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestService {

    @RequestMapping(value = "demo", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Object[] demo(Foo foo, Bar bar) {
        return new Object[]{foo, bar};
    }

    public static class Bar {
        public Long id;
        public String bar;
    }

    public static class Foo {
        public Long id;
        public String foo;
    }
}

My (encoded) payload is:

foo=%7B%22id%22%3A123%2C%22foo%22%3A%22foo1%22%7D&bar=%7B%22id%22%3A456%2C%22bar%22%3A%22bar1%22%7D

Decoded payload:

foo={"id":123,"foo":"foo1"}&bar={"id":456,"bar":"bar1"}

Request Headers:

Content-Type: application/x-www-form-urlencoded

With the above code, it returns:

[{"id":null,"foo":null},{"id":null,"bar":null}]

But what I want is:

[{"id":123,"foo":"foo1"},{"id":456,"bar":"bar1"}]

Thanks

Freddy Boucher
  • 1,387
  • 1
  • 16
  • 27

1 Answers1

0

You are creating static inner class in your RestController. Spring wont be able to auto map the properties from received request with the mentioned bean. Please define your bean in separate package or outside controller. Then you will be able to map it with @RequestBody.

            @RestController
            public class RestService {

                @RequestMapping(value = "demo", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
                public Object[] demo(@RequestBody FooBar foobar) {
                      // your custom work
                }
            }


                public class Bar {
                    public Long id;
                    public String bar;
                }

                public class Foo {
                    public Long id;
                    public String foo;
                }

// created wrapper as @RequestBody can be used only with one argument.
                public class FooBar {
                      private Foo foo;
                      private Bar bar;
                }

for referece please check requestBody with multiple beans

also ensure that the request params names are matching with the properties of your bean.(i.e Foo and Bar).

Sangam Belose
  • 4,262
  • 8
  • 26
  • 48
  • Hi @Sangam Belose. Beans defined as inner class isn't the problem but anyway I tested your solution but it doesn't work. I get the following error: {"timestamp":1497515020279,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported","path":"/demo"} – Freddy Boucher Jun 15 '17 at 08:24
  • Oh I see, that's smart! But still doesn't work: {"timestamp":1497569272670,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported","path":"/demo"} – Freddy Boucher Jun 15 '17 at 23:29