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.
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
/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)
Related
I need to consume a request which I'm not in control of. When the client posts an array ['a', 'b', 'c'] as x.
What I actually receive is:
?x.0=a&x.1=b&x.2=c
I think the correct encoding of that is ?x=a&x=b&x=c but as mentioned I'm not in control of the request.
Is there some sort of interception I can do to fix up my x.index request into the expected encoding? I've tried writing a Converter but I could only persuade that to map a single parameter into another type.
You could implement your own HandlerMethodArgumentResolver and bind the x parameter from HttpServletRequest object. Take a look at: How to implement custom parameter mapping in Spring MVC.
If it's a single endpoint that handles the strange parameter convention it would be more readable to just have HttpServletRequest as method parameter and do the work there before calling service.
Honestly I'd rather force the client to change the request structure. It doesn't look like a valid syntax for passing array with GET.
I would like to imbue my API with the ability to set values on a range of objects in a single call. A seemingly logical solution is for the user to define in the ranges in the URI and set the desired new values in the request body. For example the URI would be:
/api/horses/?color=brown
In the request body, the key/value pair would be
key: color
value: red
This will turn all of our brown horses red.
The problem is that getting parameters from the HttpServletRequest object does not let you determine if the parameters were set in the URI or the body.
Perhaps there are two questions here:
If there is different, generally accepted RESTful way to set values on a range of objects in a single call, what is it?
If using both URI and request body parameters is ok, how do you differentiate between those in the HttpServletRequest object?
Update: The accepted answer addresses how to differentiate (the second of the enumerated questions). To address the general challenge for updating a range of values, I decided that this can be best accomplished by limiting to only one updated field and specifying the field in the path. Like so:
PUT /api/horses/color?from=brown&to=red
So your problem is that you have a name clash between parameters encoded in the request line and parameters encoded in the POST body.
You can always differentiate by calling:
HttpServletRequest.getQueryString()
HttpServletRequest.getInputStream()
obviously you then have to use a URL library for parsing the query string (the encoded/decoder in the JDK is ok) and library for the form data, depending on the encoding (I recall some Apache project called commons-codec for this).
I'd go the simplest route of not having name clashes - for example by prefixing URL parameters with filter- but there's no standard rule for this.
We are facing issue related with making a path parameter optional.
original URL /expire/{token}
What we are trying to do is have the same service work for the URL's below.
1. /expire/{token}
2. /expire
Ex:- #Path("/expire/{token}")
We have already applied other solutions from SO,but no luck so far.
What about adding another method annotated with only:
#Path("/expire")
And let this method pass a null value into the original method.
Logically, it doesn't seem to make sense to have it optional. Your URI should handle the type of request it's supposed to do. But, I came across a post to make the #PathParam to be optional with a small hack using regular expressions.
http://www.nakov.com/blog/2009/07/15/jax-rs-path-pathparam-and-optional-parameters/
I would go with having separate endpoint method in Controller where it can pass the call to your services with optional parameter.
We can use regular expressions,
"#Path("/add/{a:([0-9.]*)}/{b:([0-9.]*)}{p:/?}{c:([0-9.]*)}")"
here path can be
add/2/3
add/2/3/4
my case I am using regular expressions for allowing only numbers
I know that if a parameter looks like this:
#RequestParam("userId") String userId
I can get the value by calling this:
requestParam.value()
But if I don't specify a name, Spring automatically uses the variable name. Like this:
#RequestParam String userId
If the param name isn't specified, how can I access it? I know its possible because Spring does it somehow, but requestParam.value() is blank in this case.
Spring doesn't populate the request based on the #RequestParam. Rather it populates the method argument annotated with #RequestParam (in a method annotated with #RequestMapping). The parameter given to the #RequestParam annotation tells Spring the name of the request parameter you want it to use as that argument. If you don't provide one, Spring defaults to using the name of the argument (so the 2 examples you give in your question are equivalent).
If you are trying to access a request parameter, you need to know the name of it, regardless of the framework you are using.
Recommend you not to do it that way.Spring use asm
to analysis the java bytecode file,and extract parameter name of a method.But some time this does not work,because some .class file does not comprise parameter name,just parameter types,it depends on compiler options.
In case of Java 8, Spring uses StandardReflectionParameterNameDiscoverer, which relies on Java 8 Parameters API.
For details how to set up parameters option and use it via reflection see this answer.
Just call userId, as in doSomething(userId). Spring binds everything up for you.
I'm trying to make a set of custom tags that encapsulate form elements (markup and validation).
There's a method given to retrieve the "Out" object easily:
JspWriter out = getJspContext().getOut();
However I can't figure out how to get the request object. I want to be able to directly access the submitted form values from within the Tag class so that I can validate each field.
The documentation is quite sparse, so I thought maybe I could use the JspContext object to somehow get the request attributes. But I don't understand the different scopes.
System.out.println(getJspContext().findAttribute("field1"));
always prints "null".
Enumeration e = getJspContext().getAttributeNamesInScope(1);
Looping through and printing out the enumeration just gives me a list of classes that don't exist:
javax.servlet.jsp.jspOut
javax.servlet.jsp.jspPage
javax.servlet.jsp.jspSession
javax.servlet.jsp.jspApplication
javax.servlet.jsp.jspPageContext
javax.servlet.jsp.jspConfig
javax.servlet.jsp.jspResponse
javax.servlet.jsp.jspRequest
So is this even possible?
If not, could anyone point me to a tag library that deals with form display and validation? I searched the internet for a couple hours and it seemed every single one was discontinued and I couldn't download them. Either that or suggest a better alternative for handling forms.
Edit: The tags extend the SimpleTagSupport class.
If your class is extending TagSupport, you can access the protected pageContext variable. From that you're able to retrieve the request object.
http://java.sun.com/webservices/docs/1.5/api/javax/servlet/jsp/tagext/TagSupport.html#pageContext