3

How to distinguish URL without value like this /url?var from /url?var="" in Spring MVC?

Method HttpServletRequest::getParameterMap() in controller returns "" in both ways.

I need this to separate commands from queries to specified resource.

Mati
  • 2,476
  • 2
  • 17
  • 24

2 Answers2

2

One simple way of going about doing what you want to is use the getQueryString() of HttpServletRequest. You would just check and see if the returned String contains the pattern you are looking for.

If you need something like that often (as in many controller methods) you could also easily create a custom HandlerMethodArgumentResolver that will indicate the presence of a String in the URL.

Here is the relevant Javadoc and here is an example

Community
  • 1
  • 1
geoand
  • 60,071
  • 24
  • 172
  • 190
0

/url?var is a valid URL which states that you have a parameter var which is not initialized.

So by default it is initialized to empty string. That's the framework behavior.

If you don't want to see that parameter coming in HttpServletRequest::getParameterMap(), just don't use it with URL (i.e. /url should be your call)

Maas
  • 1,317
  • 9
  • 20
  • That wouldn't solve my problem. I'm looking for method, that would for example return map of parameters and those parameter like ```/url?car``` would return ```null``` - ```/url?query=asd&run -> ["query":"asd", "run":null]``` . – Mati Aug 08 '14 at 13:37