如何在我的@RestController中映射多个豆?如何在Spring @RestController中映射多个bean?
我用弹簧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"}
请求报头:
Content-Type: application/x-www-form-urlencoded
与上面的代码,它返回:
[{"id":null,"foo":null},{"id":null,"bar":null}]
但我想要的是:
[{"id":123,"foo":"foo1"},{"id":456,"bar":"bar1"}]
感谢
可能的重复:https://stackoverflow.com/questions/20622359/automatic-conversion-of-json-form-parameter-in-spring-mvc-4-0 – chuckskull
@Freddy Boucher请检查我的编辑。 –