开发者

Request handlers for one URL can't be in different Controllers

开发者 https://www.devze.com 2023-04-05 01:29 出处:网络
In my spring mvc (Spring 3.0.2) application I have two different controller methods for handling the same url pattern but with different methods (GET and SET).

In my spring mvc (Spring 3.0.2) application I have two different controller methods for

handling the same url pattern but with different methods (GET and SET).

If I have both of them in single controller class it works fine:

@Controller
class MainController {
    @RequestMapping(value="/somepage", method=RequestMethod.GET)
    public String getMethod() {
        return "redirect:/get";
    }
    @RequestMapping(value="/somepage", method=RequestMethod.POST)
    public String postMethod() {
        return "redirect:/post";
    }
}

but it doesn't work when these methods are defined in two different controller cla开发者_JAVA技巧sses.

In this case only one method works while another gives me 405 error

(request method is not supported)

So the question is why, and how can I make it work as expected?

P.S.: Not working code:

@Controller
class GetController {
    @RequestMapping(value="/somepage", method=RequestMethod.GET)
    public String getMethod() {
        return "redirect:/get";
    }
}
@Controller
class PostController {
    @RequestMapping(value="/somepage", method=RequestMethod.POST)
    public String postMethod() {
        return "redirect:/post";
    }
}


EDIT :

It seams it is OK with 3.1.0.M2, where mappings are done by everything from @RequestMapping annotation.

So the best option for you is to change to newest Spring version.


On Spring 3.0.5 this error occurs while beans instantiation:

java.lang.IllegalStateException: Cannot map handler 'postController' to URL path [/somepage]: There is already handler of type [class test.GetController] mapped.

As far as I can see from logs and code, @Controller beans are mapped to urls only, while instantiation is done, and method dispatching is done later.


Looks like a bug to me. Try ugprading to 3.0.5 or the latest 3.1 milestone. If the problem persists, create an issue in spring's JIRA.

0

精彩评论

暂无评论...
验证码 换一张
取 消