向后兼容方法参数更改

时间:2016-08-17 10:54:58

标签: java spring

我有以下方法(1):

@RequestMapping(value = "/comments", method = RequestMethod.POST)
@ResponseBody
public Comment save(@RequestBody Comment comment) throws UnauthorisedException {
    ...
}

需要更改为(2):

@RequestMapping(value = "/comments", method = RequestMethod.POST)
public HttpEntity<Comment> save(@RequestParam(value = "comment", required = false) String comment,
                    @RequestParam("id") String id,
                    @RequestParam(value = "attachment", required = false) MultipartFile attachment,
                    @RequestParam(value = "attachmentThumbnail", required = false) MultipartFile attachmentThumbnail,
                    @RequestParam(value = "attachmentType", required = false) MediaType attachmentType) throws UnauthorisedException {
    ...
}

但是,将save方法更改为此是与先前版本相比的重大变化。如何确保/comments(1)同时适用(2)?我可以在映射前添加版本号,例如/v2/comment但这会很快产生大量不必要的代码。

Comment类看起来像这样:

@Document
public class Comment {

    @NotNull
    @Id
    private String id;

    @NotNull
    private Date timestamp;

    @NotNull
    private String comment;

    @NotNull
    private String id;

    @NotNull
    @JsonIgnore
    private String idOfUser;

    // Optional Attachment variables
    private MediaType commentAttachmentType;
    private CommentFile commentAttachmentVideo;
    private CommentFile commentAttachmentVideoThumbnail;

    private CommentFile commentAttachmentImage;
    private CommentFile commentAttachmentImageThumbnail;

    public Comment() {
        setTimestamp(new Date());
    }

    // Getters and setters for each variable
}

0 个答案:

没有答案