Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Controller Separating GET and POST mappings

I am using Spring MVC with annotation configuration. I have a controller class for handling HTTP GET calls:

@Controller
@RequestMapping("/form")
public class FormController {

    @RequestMapping(value = "/{table}/{identifier}/edit", method = RequestMethod.GET)
    public ModelAndView getEditView(ModelMap map, @PathVariable String table, @PathVariable Object identifier) {
        //generate the view for this record
    }

and a Controller for processing form submits on that URL

@Controller
@RequestMapping("/form")
public class FormSaveController {

    @RequestMapping(value = "/{table}/{identifier}/edit", method = RequestMethod.POST)
    public ModelAndView saveView(WebRequest request, @PathVariable String table, @PathVariable Object identifier) {
        //save the updated values and redirect to view
    }

When I attempt to startup my container, spring complains

Caused by: java.lang.IllegalStateException: Cannot map handler 'FormSaveController' to URL path [/form/{table}/{identifier}/edit]: There is already handler of type [class com.company.web.FormController] mapped.

This seems to indicate what I'm trying to do is not supported in Spring. The reason I am trying to separate the controller for generating the form from the controller saving the form because I am using Springs @ExceptionHandler to handle any runtime exceptions that occur, and I would like to handle an exception for displaying the view differently than an exception for saving a record.

Is there a different way of handling what I am trying to do (utilize Springs @ExceptionHandler annotation for specific kinds of request?)

like image 399
BuffaloBuffalo Avatar asked May 31 '26 12:05

BuffaloBuffalo


1 Answers

Have you tried using in the same Class? I think that would work. If you wish to use ExceptionHandler then try HandlerExceptionResolver

like image 53
Abhishek Avatar answered Jun 03 '26 01:06

Abhishek