0

I have controller:

@RestController
public class MyController {

    @GetMapping(value = "/products/{value}")
    public String get(@PathVariable String value) {
        System.out.println(value);
        return "OK";
    }
}

After start server I try to send a message like this:

http://localhost:8080/products/Mazda

and I see in console Mazda. But when I send value with a backslash:

http://localhost:8080/products/Mazda\6

I get an error:

This application has no explicit mapping for /error, so you are seeing this as a fallback.

How can I pass a value with '\' symbol as get parameter to my controller?

I expect: Mazda\6

Marko Previsic
  • 1,820
  • 16
  • 30
ip696
  • 6,574
  • 12
  • 65
  • 128

1 Answers1

0

First, you need to encode the path variable on the client side. Here is an example for Java:

URLEncoder.encode("Mazda\\6", "UTF-8");

The result will be Mazda%5C6. The second step is to allow the processing of requests containing special characters in their URLs.

Third, decode the string in the controller:

String decodedValue = URLDecoder.decode(value, "UTF-8");
Sergii Zhevzhyk
  • 4,074
  • 22
  • 28