通过邮寄发送对象

时间:2012-01-04 19:16:10

标签: spring rest spring-mvc

我想创建一个表单,我可以在REST应用程序中更新我的实体。例如,我有一个用户实体,用户名和真实姓名字段。

我的请求方法中是否需要执行此类操作

    @RequestMapping(value = "/admin/user/update", method = RequestMethod.POST)
public String updateHouse(@RequestBody String username, @RequestBody String realname, Model model) 

...

我更愿意做这样的事情

@RequestMapping(value = "/admin/house/update", method = RequestMethod.POST)
public String updateHouse(@RequestBody User user, Model model) 

我的意思是我想发送一个不是每个字段的对象。如果我的实体中有20个字段,我必须在我的方法中添加20个参数。这不是想要的。

我正在使用弹簧形式标签

-------更新

感谢您的回复。下面是不同的实体,但我试图开始的实际情况

html代码

<c:url var="houseUpdateLink" value="/admin/house/update" />
<form:form method="post" commandName="house" action="${houseUpdateLink}">
    <form:input path="house.title" />
<input type="submit" value="send" />
</form:form>

控制器方法

@RequestMapping(value = "/admin/house/update", method = RequestMethod.POST)
public String updateHouse(@RequestBody House house, Model model) {
    model.addAttribute("step", 3);

    logger.info("test: " + house.getTitle());
    return "houseAdmin";
}

我收到了

HTTP Status 415 -

type Status report

message

description The server refused this request because the request entity is in a format     not supported by the requested resource for the requested method ().

2 个答案:

答案 0 :(得分:1)

此处您不需要@RequestBody。如果没有它,Spring会自动将表单字段绑定到House类,即

@RequestMapping(value = "/admin/house/update", method = RequestMethod.POST)
public String updateHouse(House house, Model model) {

答案 1 :(得分:-1)

在Spring表单标签中,您可以执行user.username,并将User对象作为参数传递。