Mati
Mati

Reputation: 2576

Distinguish empty value from empty string ("") in Url query parameter

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.

Upvotes: 3

Views: 5386

Answers (2)

geoand
geoand

Reputation: 64011

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

Upvotes: 3

Maas
Maas

Reputation: 1357

/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)

Upvotes: 0

Related Questions