3

We are using Spring Boot to build rest api, I am facing issue in @PathVariable in Spring Boot rest api. It is not accepting trialing whitespace in path parameter in the URL.

This URL works:

https://foo.com/api/v1/pen/?param1=foobar

But below url doesn't work, and it removes trailing whitespace:

https://foo.com/api/v1/pen /?param1=foobarfoo

Yes, it may look strange but we have requirements as this api does lot of database search and even whitespace with without whitespace affects the output. This is how my Request Mapping looks like:

@RequestMapping(value="api/v1/{sentenceToSearch}",method=RequestMethod.GET,produces={MediaType.APPLICATION_JSON})

I searched many questions in stackoverflow like:

Controller Pathvariable bind removes ending space

But above question doesn't have solution. Also tried various questions which was having query to prevent trimming of "." from url and based on that I tried to add below code:

@Configuration
  protected static class AllResources extends WebMvcConfigurerAdapter {

      @Override
      public void configurePathMatch(PathMatchConfigurer matcher) {
          matcher.setUseRegisteredSuffixPatternMatch(true);
      }

  }

But it works for "." not for special characters, also I tried adding expression like this in request mapping: {sentenceToSearch:.+} but all of them not working.

Can someone help me in this?

Community
  • 1
  • 1
Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
  • Then send some special character instead of whitespace, generate a random string, and use that instead of a whitespace. Adjusting your code to accept whitespace is more complex than replacing whitespaces with a random string. # – We are Borg Jul 14 '16 at 11:17
  • 1
    Path with white space is not a valid path. Use params for your search terms. – alex Jul 14 '16 at 11:21
  • Thanks for input people! you mean there is no way to accept white space as character? Because i'm not sure if sending some special character is a solution because it requires many changes on client side! – Pradeep Simha Jul 14 '16 at 11:25
  • yes there is no way to do that directly...but on the client side can do things like var `url = "https://foo.com/api/v1/"+encodeURIComponent(varWithSpecialCharaters) +"/?param1=foobarfoo"`, if is javascript client, but sure can something like that for other clients – Mosd Jul 14 '16 at 12:10

0 Answers0