1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

3 years later I have the same need, I can't find any example on how to implement this. Could you give an example ?
Hi @Michal Please share the example for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.