14

Am sending data from JSP to controller using query string.

My controller is annotation driven.

The value of the the request parameter should be case-insensitive.

The method which i use for welcome page is

public String welcome(@RequestParam("orgID") String orgID, ModelMap model)

The request parameter "orgID" should be case insensitive. How to do this ?.

I should be able to give the query-string as "orgid" or "orgId". The parameter should be completely case-insensitive. Looking for your help friends.

Thanks in Advance :-)

Arun
  • 3,440
  • 11
  • 60
  • 108
  • 2
    If this request parameter name had to be entered manually by an end-user, I would understand why this would be desirable. But it's your own code that should generate appropriate query strings. Why don't you simply respect the expected param name. Choose naming conventions and stick to them. – JB Nizet Jan 19 '12 at 07:36
  • 1
    Actually, two to three external applications access this URL. One would give the query-string as "orgID", one would give as "orgId" and another one would give as "orgid" and another one as "Orgid". Cannot expect how many requests would give and in how many patterns. So I wanted to have it as case-insensitive :-) – Arun Jan 19 '12 at 08:51
  • 1
    And why not fixing these apps? Once you'll have case-insensitive params, they will use the wrong URL. If the developers of these external apps are unable to respect such a simple contract, you shouldn't fix anything, and they should learn how to do their job. – JB Nizet Jan 19 '12 at 08:55
  • 1
    I'm with @JBNizet on this one. Once you go down the route of trying to cope with what the lazy people do, then you will find your code is polluted with workarounds. It's better to train your users to use the right contract if they want the right answer! – David Newcomb Sep 23 '16 at 15:37

6 Answers6

4

Another approach would be to have two parameters "orgId" and "orgid" and have the optional.

public String welcome(@RequestParam(value="orgID", required = false) String org_ID, @RequestParam(value="orgid", required=false, String orgid, ModelMap model) {
final String orgId = orgid == null ? org_ID : orgid;
...
}

But if you have control over the parameters I would strongly prefer to just have one consistent way, say org-id and follow it both in the client and the server side.

Neo M Hacker
  • 899
  • 7
  • 11
3

It might be nice for Spring to support this, but I can also understand why they wouldn't since it could result in a weakening of an interface contract between a supplier and consumer. In the meantime, here's a fairly straightforward way to take the first value using the parameter name regardless of its case.

    String myVal = null;
    for (String pname : request.getParameterMap().keySet() ) {
        if ( pname != null && pname.toLowerCase().equals("your_lc_pname") ) {
            for (String v : request.getParameterValues(pname) ) {
                // do something with your value(s)
            }
        }
    }
beaudet
  • 886
  • 1
  • 10
  • 13
  • Can also be optimized for multiple Request Parameters as well. The problem occurs when any of the Request Parameters received in the RestController are Optional. – Radhesh Khanna Sep 04 '20 at 09:19
3

You'll have to try changing the way Spring matches your urls . You could for one, create a filter (probably a DelegatingFilterProxyBean) to lower case your parameter before you pass it on to Spring or try to change the way the paths are matched .

An explanation to the second options is given at How can I have case insensitive URLS in Spring MVC with annotated mappings .

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
Aravind A
  • 9,507
  • 4
  • 36
  • 45
  • Hi Aravind. If am not wrong, the link which u pasted here is telling how to make the path case-insensitive and not particular parameter – Arun Jan 19 '12 at 08:53
  • 1
    @Arun yup . I guess the path will include the parameter . Else you may have to lowercase it using a filter or something . – Aravind A Jan 19 '12 at 08:56
  • I tried as adviced in that link. But it includes only the path and not the parameter :-(. Have to look for someother option now :-( – Arun Jan 19 '12 at 09:09
  • 1
    The filter approach works fine (thanks for the advice here!). Here's my writeup with config and code - http://www.acooke.org/cute/Forcinglow0.html – andrew cooke Sep 07 '13 at 04:51
2

in Spring 4.2+

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="org.saat")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new 
InternalResourceViewResolver();
        viewResolver.setPrefix("/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}
2

The simplest way I found was to change the controller method to take a Map with a single key value pair as a parameter and then convert the key to lowercase.

public String welcome(@RequestParam(required = true) Map<String, String> params, ModelMap model) {
   String caseInsensitiveOrgId = params.keySet().toArray()[0].toString().toLowerCase();
   String OrgValue = params.values().toArray()[0];
   // Do whatever you want here
}
Pat
  • 71
  • 5
1

There is a simple workaround.You can operate directly on the HttpServletRequest and use method getParameter() and check all versions of parameter.

public String welcome(HttpServletRequest request, ModelMap model){
   String orgID = extractOrgId(request);
   //rest of your code
}

private String extractOrgId(HttpServletRequest request){
   if(request.getParameter("orgId") != null){
       return request.getParameter("orgId");
   }
   // and so on
}
randomPerson
  • 116
  • 3
  • Very nice work around :-). I use this solution as of now. But I would be happy to any other solution in spring, which will take the incoming request parameters and convert in to lower case and then process further ... – Arun Jan 20 '12 at 05:14