Spring MVC RequestMapping ParamRequest collection/array - java

I have a #Controller with a #RequestMapping functions that accept collections.
Imagine something like:
requestHandler(Collection<Long> param){
...
}
This mapping only matches when I send requests such as:
http://www.domain.com/mapping/funct?param=1&param=2&param=3
I'd like to match it as well when I send a comma sepparated value:
http://www.domain.com/mapping/funct?param=1,2,3
Is there a way without using .split ? I'd like it to be automatically parsed to a collection.

You would have to write a custom Converter and register it in your Spring MVC context.

It is better to access all query parameters and parse according to your needs in this type of scenarios
You should have access to the requests query string via request.getQueryString().
In addition to getQueryString, the query parameters can also be retrieved from request.getParameterMap() as a Map.

Related

SpringMVC - Naming strategy for #RequestParam

I would like all my APIs to use lower-case request parameters, but I would still like to use camel-case in my Java code.
For example, consider the following code:
#GetMapping("/ping")
public String ping(
String responseMessage)
{
return "PONG " + responseMessage;
}
To contact this endpoint and supply a response message, I would have to call: localhost:8080/ping?responseMessage=Hello. However, I would like spring to automatically bind the variable name responseMessage to a request parameter named responsemessage. I specifically do not want to manually write #RequestParam(name="responsemessage") in my controller, but rather configure a global naming strategy for request parameters. Is this possible?
There are a couple of other threads on SO that talks about making the request parameters or url mappings case-insensitive:
RequestParam value in spring MVC to be case insensitive
Is there any way we make PathVariable name case insensitive in Spring?
Spring mvc. case insensitive get parameters mapping
Making a request parameter binding case insensitive
However, the answers in these questions strikes me as quite hacky or requires manual codeing. Is there no simple setting for the behaviour I am after? I know this is possible when using JSON bodies, by configuring the Jackson propertyNamingStrategy. Is there an equivalent for request parameters?

How to get spring post param in a form with any name

I want to get parameter from POST method in Spring with name that starts with "value". And it can be like "value_345465". Any ideas?
There is nothing Spring MVC specific. You can use ServletRequest.getParameterNames(), ServletRequest.getParameterMap(), and ServletRequest.getParameter() methods to accomplish this task.

Spring boot and validation of param in url

#RequestParam(value = "param") String param
How to validate this param in elegant way ? More exactly I must check if param is some value in database. My stack is: spring-boot+mybatis
This might be a duplicate of that one but anyway.
There's a difference in the way you validate forms and separate parameters. With POST it's actually impossible to break the request into separate parameters and you get the whole post body, and you use #Valid to process it. With GET it is possible to have separate parameters as arguments in method, and in this case Spring proprietary #Validated annotation should be used.

Spring REST Controllers, get unmapped query params

Using Spring 4 to create REST controllers, I would like to return warnings in responses of my controllers if clients used unexpected query parameters.
There is a way to get all query parameters (http://stackoverflow.com/questions/7312436), but what I would like is for Spring to provide all query parameters that have not been been mapped to method params by Spring already.
Then I would decide how to treat them, e.g. ignore a some whitelisted query params that have meaning for the client or intermediates, return error when the old name of a parameter was used, return a warning in the response for all other query parameters such that human users can see if their assumption about the query param name was wrong.
So is there an easy way to get all query params from the request that have not been defined using #RequestParam()?
No possibility forthcoming, I opened a ticket:
https://jira.spring.io/browse/SPR-14019

Can I find the URL for a spring mvc controller in the view layer?

I think what I need is called reverse url resolution in Django. Lets say I have an AddUserController that goes something like this:
#Controller
#RequestMapping("/create-user")
public class AddUserController{ ... }
What I want is some way to dynamically find the url to this controller or form a url with parameters to it from the view (JSP), so I don't have to hardcode urls to controllers all over the place. Is this possible in Spring MVC?
Since Spring 4 you can use MvcUriComponentsBuilder.
For the most type-safe method:
String url = fromMethodCall(on(MyController.class).action("param")).toUriString();
Note this example requires that the method returns a proxyable type - e.g. ModelAndView, not String nor void.
Since 4.2, the fromMappingName method is registered as a JSP function called mvcUrl:
Login
This method does not have the proxy restriction.
Have you considered having a bean that aggregates all of the controller URLs you need into a HashMap and then adding this controller/URL Map to any model that requires it? Each Spring controller has the ability to call an init() method, you could have each controller add it's name and URL to the controller/URL map in the init() methods so it would be ready to use when the controllers go live.
Can solve with Java Reflection API. By Creating Custom Tag library. methods looks like this
Class c = Class.forName("Your Controller");
for(Method m :c.getMethods()){
if(m.getName()=="Your Method"){
Annotation cc = m.getAnnotation(RequestMapping.class);
RequestMapping rm = (RequestMapping)cc;
for(String s:rm.value()){
System.out.println(s);
}
}
}
Possible Problem You Can Face is
1.Path Variable > Like this /pet/show/{id} so set of path name & value should be support then replace this String.replace() before return url
2.Method Overriding > only one method is no problem. if Method override Need to give support sequence of Parameter Type That you really want like Method.getParametersType()
3.Multiple Url to Single Method> like #RequestMapping(value={"/", "welcome"}). so easy rule is pick first one.
4.Ant Like Style Url > Like this *.do to solve this is use multiple url by placing ant like style in last eg. #RequestMapping(value={"/pet","/pet/*.do"})
So Possible link tag style is
<my:link controller="com.sample.web.PetController" method="show" params="java.lang.Integer">
<my:path name="id" value="1" />
</my:link>
Where parmas attribute is optional if there is no method override.
May be I left to think about some problem. :)
I would probably try to build a taglib which inspects the annotations you're using in order to find a suitable match:
<x:url controller="myController">
<x:param name="action" value="myAction"/>
</x:url>
Taglib code might be something roughly like
Ask Spring for configured beans with the #Controller annotation
Iterate in some suitable order looking for some suitable match on the controller class or bean name
If the #RequestMapping includes params, then substitute them
Return the string
That might work for your specific case (#RequestMapping style) but it'll likely get a bit hairy when you have multiple mappings. Perhaps a custom annotation would make it easier.
Edit:
AbstractUrlHandlerMapping::getHandlerMap, which is inherited by the DefaultAnnotationHandlerMapping you're most likely using, returns a Map of URL to Handler
Return the registered handlers as an
unmodifiable Map, with the registered
path as key and the handler object (or
handler bean name in case of a
lazy-init handler) as value.
So you could iterate over that looking for a suitable match, where "suitable match" is whatever you want.
You can get access to the request object in any JSP file without having to manually wire in or manage the object into the JSP. so that means you can get the url path off the request object, have a google into JSP implicit objects.
Here is a page to get you started http://www.exforsys.com/tutorials/jsp/jsp-implicit-and-session-objects.html
The problem with this is that there's no central router in SpringMVC where all routes are registered and ordered. Then reverse routing is not a static process and route resolution in the view layer can be hard to integrate.
Check out this project for a centralized router (like rails) and reverse routing in the view layer.

Categories