Spring REST 多个控制器用于一个 URL 但不同的 http 方法

发布于 2024-11-27 10:21:40 字数 1253 浏览 6 评论 0原文

我目前有一个控制器可以处理 URL 组的 GET 和 POST:

@Controller
public class RestGroups {

...

    @RequestMapping(method = RequestMethod.GET, value = "/groups")
    @ResponseBody
    public GroupsDto groups() {
        return new GroupsDto(getGroups());
    }

    @RequestMapping(method = RequestMethod.POST, value = "/groups", headers = "Accept=application/xml")
    @ResponseBody
    public GroupsDto postGroup(@RequestBody GroupDto groupDto) {
        groupSaver.save(groupDto.createEntity());
        return groups();
    }

现在我想要两个控制器,两个控制器都分配给相同的 URL,但每个控制器分配给不同的方法,如下所示:

@Controller
public class GetGroups {

...

    @RequestMapping(method = RequestMethod.GET, value = "/groups")
    @ResponseBody
    public GroupsDto groups() {
        return new GroupsDto(getGroups());
    }

...

}


@Controller
public class PostGroup {

...


    @RequestMapping(method = RequestMethod.POST, value = "/groups", headers = "Accept=application/xml")
    @ResponseBody
    public GroupsDto postGroup(@RequestBody GroupDto groupDto) {
        groupSaver.save(groupDto.createEntity());
        return groups();
    }

...
}

可能吗?因为现在我得到 Spring 异常,一个 URL 无法由两个不同的控制器处理。这个问题有解决方法吗?我真的很想将这两个完全不同的操作分成两个单独的类。

I currently have one controller that handles both GET and POST for URL groups:

@Controller
public class RestGroups {

...

    @RequestMapping(method = RequestMethod.GET, value = "/groups")
    @ResponseBody
    public GroupsDto groups() {
        return new GroupsDto(getGroups());
    }

    @RequestMapping(method = RequestMethod.POST, value = "/groups", headers = "Accept=application/xml")
    @ResponseBody
    public GroupsDto postGroup(@RequestBody GroupDto groupDto) {
        groupSaver.save(groupDto.createEntity());
        return groups();
    }

Now I would like to have TWO controllers, both assigned for same URL but each for different method, something like below:

@Controller
public class GetGroups {

...

    @RequestMapping(method = RequestMethod.GET, value = "/groups")
    @ResponseBody
    public GroupsDto groups() {
        return new GroupsDto(getGroups());
    }

...

}


@Controller
public class PostGroup {

...


    @RequestMapping(method = RequestMethod.POST, value = "/groups", headers = "Accept=application/xml")
    @ResponseBody
    public GroupsDto postGroup(@RequestBody GroupDto groupDto) {
        groupSaver.save(groupDto.createEntity());
        return groups();
    }

...
}

Is it possible? Because now I get Spring exception that one URL cannot be handled by two different controllers. Is there a workaround for this issue? I really would like to separate those two completely different actions into two separate classes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2024-12-04 10:21:40

Spring 3.1 中通过新的 HandlerMethod 抽象解决了这个限制。您必须升级到 3.1.M2。如果您需要示例,请告诉我。

This limitation has been solved in Spring 3.1 with its new HandlerMethod abstraction. You'll have to upgrade to 3.1.M2. Let me know if you need an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文