In a Spring controller, can I have a method called based on the number of request parameters?

I've been retrofitting an existing webapp with Spring. Clearly it's easier to start with Spring than to add it on later.

We have servlets that can take multiple request parameters. Based on the number of parameters different actions will be taken. For example,

/doSomething?prod=15

displays the information for product 15 and

/doSomething?prod=15&owner=99

sets the owner of product 15 to 99 and

/doSomething?prod=15&delete=y

deletes product 15.

I have a controller working but I don't know how to call different methods based on the number of parameters. For example, this works (trivial method just to ensure I have the basics working):

@RequestMapping(method=RequestMethod.GET)
public ModelAndView doIt(@RequestParam("prod") int prod, Model model)
{
  ModelAndView mav = new ModelAndView();
  mav.setViewName("jsonView");
  return mav;
}

but not this:

@RequestMapping(method=RequestMethod.GET)
public ModelAndView doIt(@RequestParam("prod") int prod, Model model)
{
  ModelAndView mav = new ModelAndView();
  mav.setViewName("jsonView");
  return mav;
}

@RequestMapping(method=RequestMethod.GET)
public ModelAndView doIt(@RequestParam("prod") int prod,
                         @RequestParam("owner") int owner,
                         Model model)
{
  ModelAndView mav = new ModelAndView();
  mav.setViewName("jsonView");
  return mav;
}

That throws an IllegalStateException, "Ambiguous handler methods mapped for HTTP path '/mytest'" and goes on to state:

If you intend to handle the same path in multiple methods, then factor them out into a dedicated handler class with that path mapped at the type level!

I don't understand what that message is telling me to do.

If I set up the method to accept more or different parameters than are passed in I get "The request sent by the client was syntactically incorrect()".

Paul

Answers


The RequestMapping annotation tells Spring which URL requests to map to your controller. You can put the value either at the method level or at the class level.

In your example, nothing differs between the two request mappings. You can do this, though:

@RequestMapping(value="/bar/example.htm", method={RequestMethod.GET}, params={"prod", "owner"})
public String doIt( @RequestParam("prod") int prod,
                    @RequestParam("owner") int owner) {
    LOGGER.trace("in doIt(int,int)");
    return "foo/bar";
}
@RequestMapping(value="/bar/example.htm", method={RequestMethod.GET}, params={"prod"})
public String doIt( @RequestParam("prod") int prod) {
    LOGGER.trace("in doIt(int)");
    return "foo/bar";
}

You can even share a model between them if you want:

@Model
public static Map<String,Object> model() {
  LOGGER.trace("in model()");
  Map<String,Object> model = new HashMap<>();
  model.put("hello", "hello world");
  return model;
}

Your request mapping needs to map an actual URL rather then just the HTTP method. You can also put required params on a per method basis like so...

@RequestMapping(value="/doSomething", method=RequestMethod.GET, params=["prod", "owner"])
public ModelAndView doIt(@RequestParam("prod") int prod,
                         @RequestParam("owner") int owner,
                         Model model)
{
  ModelAndView mav = new ModelAndView();
  mav.setViewName("jsonView");
  return mav;
}

Need Your Help

How do I correctly use setInterval and clearInterval to switch between two different functions?

javascript animation counter setinterval clearinterval

For practice I am trying to display a number that increments from 0 - 9, then decrements from 9 - 0, and infinitely repeats.The code that I have so far seems to be close, but upon the second iterat...

Are shell scripts sensitive to encoding and line endings?

bash shell sh

I am making a NW.js app on Mac, and want to run the app in dev mode by double-clicking on an icon. First step, I'm trying to make my shell script work.