Search code examples
javaspring-bootjava-11spring-boot-2

Getting an error having a slash in PathParameter String variable


I have a Java Spring Boot application with a couple of API REST endpoints.

In one of my endpoints I am using as a search endpoint.

My problem is calling this specific endpoint with a path variable containing a slash "/" in it will give me a HTTP 400 Bad Request. The program doesn't seen to find the current mapping at all.

ie if the search string is foo/bar then the following call gives me a HTTP status 400 - Bad Request:

curl -X PUT "http://127.0.0.1:8080/search/foo%2Fbar" \
   -H "accept: application/json;charset=UTF-8" \
   -H "Content-Type: application/json" \
   -d "{ \"someSettings\": \"some value\", \"startNumber\": 0}"

This is how this part of the program looks like.

   @PutMapping(value = "/search/{searchString}",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
   public ResponseEntity<SearchResultModel> search(@PathVariable("searchString") String searchString, @RequestBody SearchSettingsModel searchSettings) {
      return handler.doSearch(searchString, searchSettings);
   }

The search works fine when I have any other text string, and I prefer to use a PathVariable if possible. Is it possible to have the search in a PathVariable or do I have to put the string in the RequestBody instead?

I am using a Spring-Boot application with a Maven build with Java 11.


Solution

  • You can't have a %2F (= /) in your url.

    Either add this to the body or as query parameter.