1

I have API like this-

  1. /objectname/name

  2. /objectname/collection/id

Both API's are indirectly related.

Problem occurs when calling first API with name value as "A/B Type". So rest controller actually calling second API rather first (/objectname/A/B Type) because forward slash. How to deal with this situation.

As a side note I am encoding the parameters values.

I developed the restful services using SpringBoot and RestTemplate.

Sam
  • 859
  • 3
  • 12
  • 23

1 Answers1

1

The conflict comes by specifying the name directly in the resource path and passed to the function as a @PathVariable.

Your code looks something like this:

@RequestMapping(value = "objectname/{name}", method = RequestMethod.GET)
    public String yourMethodName(@PathVariable String name){
        return name;
    }

What I would recommend in order to avoid this kind of conflict is (if you're allowed to modify the @RestController or @RepositoryRestResource layers) to pass the value of the object in a @RequestParam

For instance:

@RequestMapping(value = "/objectname", method = RequestMethod.GET)
    public String yourMethodName(@RequestParam(name = "name", required = true) String name){
 return name;
}

That said, When you are constructing your the request using RestTemplate then you should url encode your name (A%2FB%20Testing) and construct the following url:

http://localhost:8080/objectname?name=A%2FB%20Testing

I tested this locally and worked alright for me.

fndg87
  • 331
  • 4
  • 13
  • Note: You can take a look at the following post http://stackoverflow.com/questions/3235219/urlencoded-forward-slash-is-breaking-url – fndg87 Nov 14 '16 at 19:56
  • as alternative to the solution I provided (passing the value as a RequestParam is easier to implement thought) – fndg87 Nov 14 '16 at 19:57
  • i got your point, i cant change API "RequestMapping". Problem is if i already have API request mapping as "/objectName". Is there any other way we can solve it? – Sam Nov 14 '16 at 21:55