Spring MVC request mapping explicitly disallowing

2020-07-24 04:20发布

问题:

I'm running into a Spring (3.1) mapping conundrum: I have a simple, findAll method:

@RequestMapping( method = RequestMethod.GET )
@ResponseBody
public List< User > findAll(){
    return findAllInternal();
}

This maps on the following URI (which is good): /user.

However it also maps on: /user?bla=8, which is not good.

Is there a way to explicitly specify the fact that this particular mapping contains no parameters? I have tried to specify the params in the @RequestMapping but I can see no clear way of doing this.

Any help is appreciated.

Thanks. Eugen.

回答1:

You can negate parameters in @RequestMapping.param attribute.

@RequestMapping( method = RequestMethod.GET, params = "!bla")

As of spring 3.1 you can't explicitly disallow request to have parameters.

But to be honest it's rarely necessary.

There are three use cases:

  1. bla is a known parameter that you can explicitly allow/disallow.
  2. bla is unknown parameter and can be ignored.
  3. you want to handle requests with unknown parameters in a special way (throw an error for example).

The first two can be done in spring mvc. The third one, AFAIK, can't.

The third option is also potentially dangerous as you'd have to be really careful about the parameters the clients can send to your sever (like stuff added by security frameworks, etc).