假设我有一个页面,其中列出了表上的对象,并且我需要放置一个表单来过滤表。过滤器作为Ajax GET发送到如下网址:http://foo.com/system/controller/action?page=1&prop1=x&prop2=y&prop3=z
而不是在我的控制器上有很多参数,比如:
@RequestMapping(value = "/action")
public @ResponseBody List<MyObject> myAction(
@RequestParam(value = "page", required = false) int page,
@RequestParam(value = "prop1", required = false) String prop1,
@RequestParam(value = "prop2", required = false) String prop2,
@RequestParam(value = "prop3", required = false) String prop3) { ... }
假设我的MyObject是:
public class MyObject {
private String prop1;
private String prop2;
private String prop3;
//Getters and setters
...
}
我想做一些类似的事情:
@RequestMapping(value = "/action")
public @ResponseBody List<MyObject> myAction(
@RequestParam(value = "page", required = false) int page,
@RequestParam(value = "myObject", required = false) MyObject myObject,) { ... }
有可能吗?我该怎么做呢?
发布于 2013-06-05 22:16:50
你完全可以这样做,只要去掉@RequestParam
注解,Spring就会干净利落地把你的请求参数绑定到你的类实例上:
public @ResponseBody List<MyObject> myAction(
@RequestParam(value = "page", required = false) int page,
MyObject myObject)
发布于 2019-02-08 21:30:41
由于在每个帖子下面都会弹出如何设置必填字段的问题,因此我写了一个小示例来说明如何根据需要设置字段:
public class ExampleDTO {
@NotNull
private String mandatoryParam;
private String optionalParam;
@DateTimeFormat(iso = ISO.DATE) //accept Dates only in YYYY-MM-DD
@NotNull
private LocalDate testDate;
public String getMandatoryParam() {
return mandatoryParam;
}
public void setMandatoryParam(String mandatoryParam) {
this.mandatoryParam = mandatoryParam;
}
public String getOptionalParam() {
return optionalParam;
}
public void setOptionalParam(String optionalParam) {
this.optionalParam = optionalParam;
}
public LocalDate getTestDate() {
return testDate;
}
public void setTestDate(LocalDate testDate) {
this.testDate = testDate;
}
}
//Add this to your rest controller class
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String testComplexObject (@Valid ExampleDTO e){
System.out.println(e.getMandatoryParam() + " " + e.getTestDate());
return "Does this work?";
}
发布于 2014-03-26 09:47:57
我也有类似的问题。实际上,问题正如我所想的那样更深。我使用的是默认使用Content-Type:application/x-www-form-urlencoded; charset=UTF-8
的jquery $.post
。不幸的是,我的系统是建立在这个基础上的,当我需要一个复杂的对象作为@RequestParam
时,我不能简单地实现它。
在我的例子中,我尝试发送用户首选项,如下所示;
$.post("/updatePreferences",
{id: 'pr', preferences: p},
function (response) {
...
在客户端,发送给服务器的实际原始数据是;
...
id=pr&preferences%5BuserId%5D=1005012365&preferences%5Baudio%5D=false&preferences%5Btooltip%5D=true&preferences%5Blanguage%5D=en
...
解析为;
id:pr
preferences[userId]:1005012365
preferences[audio]:false
preferences[tooltip]:true
preferences[language]:en
所述服务器端为;
@RequestMapping(value = "/updatePreferences")
public
@ResponseBody
Object updatePreferences(@RequestParam("id") String id, @RequestParam("preferences") UserPreferences preferences) {
...
return someService.call(preferences);
...
}
我尝试了@ModelAttribute
,添加了setter/getter,所有可能的构造器到UserPreferences
,但没有机会识别发送的数据为5个参数,但实际上映射方法只有2个参数。我也尝试了Biju的解决方案,但是发生的情况是,spring使用默认构造函数创建了一个UserPreferences对象,并且不填充数据。
我解决了这个问题,从客户端发送首选项的JSon字符串,并将其作为服务器端的字符串进行处理;
客户端:
$.post("/updatePreferences",
{id: 'pr', preferences: JSON.stringify(p)},
function (response) {
...
服务器:
@RequestMapping(value = "/updatePreferences")
public
@ResponseBody
Object updatePreferences(@RequestParam("id") String id, @RequestParam("preferences") String preferencesJSon) {
String ret = null;
ObjectMapper mapper = new ObjectMapper();
try {
UserPreferences userPreferences = mapper.readValue(preferencesJSon, UserPreferences.class);
return someService.call(userPreferences);
} catch (IOException e) {
e.printStackTrace();
}
}
为了简单起见,我在REST方法中手动进行了转换。在我看来,spring不能识别发送的数据的原因是content-type。
https://stackoverflow.com/questions/16942193
复制相似问题