【问题标题】:How to map multiple beans in Spring @RestController?如何在 Spring @RestController 中映射多个 bean?
【发布时间】:2017-06-15 06:19:26
【问题描述】:

如何在我的@RestController 中映射多个bean

我正在使用 spring-web-4.3.8.RELEASE.jar

我尝试了所有方法:@RequestParam @RequestBody、@RequestAttribute、@RequestPart 但没有任何效果...

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;
    }
}

我的(编码的)有效载荷是:

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

解码的有效载荷:

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

请求标头:

内容类型:application/x-www-form-urlencoded

使用上面的代码,它返回:

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

但我想要的是:

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

谢谢

【问题讨论】:

标签: java spring spring-mvc


【解决方案1】:

您正在 RestController 中创建静态内部类。 Spring 将无法使用提到的 bean 自动映射收到的请求中的属性。请在单独的包或外部控制器中定义您的 bean。然后你就可以用@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;
                }

参考请查看requestBody with multiple beans

还要确保请求参数名称与 bean 的属性匹配。(即 Foo 和 Bar)。

【讨论】:

  • 嗨@Sangam Belose。定义为内部类的 Bean 不是问题,但无论如何我测试了您的解决方案,但它不起作用。我收到以下错误: {"timestamp":1497515020279,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' 不支持","path":"/demo"}
  • 哦,我明白了,这很聪明!但仍然不起作用: {"timestamp":1497569272670,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content类型 'application/x-www-form-urlencoded;charset=UTF-8' not supported","path":"/demo"}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多