Restful API和SpringMVC,为方便起见,我应该将路径变量设为@RequestBody对象的字段吗?

时间:2014-03-26 10:30:29

标签: rest spring-mvc

我拥有收集反馈的申请。

api看起来像这样:

POST /customer/feedback/{feedbackId} HTTP/1.1
Host: localhost:9999
Content-Type: application/json
Content-Body: {'qaSays':'Approved', 'qaRating':98}
//This results the feedback (find by feedbackId) gets updated

使用spring-mvc构建应用程序

//in controller
@RequestMapping(value="/customer/feedback/{feedbackId}")
@ResponseStatus(OK)
public void handle(@PathVariable("feedbackId") feedbackId, 
       @RequestBody ApproveFeedbackCommand command) {
     command.setFeedbackId(feedbackId);
     commandGateway.send(command);
}

//Command
public class ApproveFeedbackCommand {
     private String feedbackId;//makes no sense for client
     private String qaSays;
     private int qaRating;
}

上面的解决方案有效,但我觉得这里出了问题。

问题是:

1.为方便起见,我应该将ApproveFeedbackCommand用作@RequestBody吗? 额外的字段" feedbackId"可能会混淆其他开发人员,手动设置路径变量很烦人。

2.我应该针对不同的问题添加ApproveFeedbackBody(排除" feedbackId")? 但这似乎添加了一些重复的代码和一些手动命令参数提取。

@RequestMapping(value="/customer/feedback/{feedbackId}")
@ResponseStatus(OK)
public void handle(@PathVariable("feedbackId") id, 
       @RequestBody ApproveFeedbackBody body) {
    ApproveFeedbackCommand command = convert(id, body);//manual convert here
    commandGateway.send(command);
}

0 个答案:

没有答案