Sam
Sam

Reputation: 869

ReSTful service getting contradict due to path parameter value has forward slash

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.

Upvotes: 1

Views: 990

Answers (1)

fndg87
fndg87

Reputation: 331

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.

Upvotes: 1

Related Questions