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.

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

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] .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.