Search code examples
javarestswaggerresttemplate

RestTemplate Swagger Not enough variable values available to expand


My method throws an exception:

 @GetMapping(value = "/orders/{email}")
    private List<Order> ordersByEmail(@PathVariable String email) {
        List<Order> list = restTemplate.exchange(
                "http://localhost:8082/api/v1/orders/email/{email}",
                HttpMethod.GET,
                new HttpEntity<>(email),
                new ParameterizedTypeReference<List<Order>>() {
                }).getBody();
       }

the request url method:

@ApiOperation("Find orders by email")
    @GetMapping(value = "/email/{email}")
    public List<Order> findOrdersByEmail(@PathVariable String email) {
        return orderService.findByEmail(email);
    }

curl:

curl -X GET "http://localhost:8084/api/v1/processor/orders/{email}?email=string" -H "accept: */*"

request url:

http://localhost:8084/api/v1/processor/orders/{email}?email=string

swagger response:

Error:
   Response body
            Download
            {
                 "timestamp": "2019-01-04T00:27:34.059+0000",
                 "status": 500,
                 "error": "Internal Server Error",
                 "message": "Not enough variable values available to expand 'email'",
                 "path": "/api/v1/processor/orders/%7Bemail%7D" 
            } 
   Response headers
            connection: close
            content-type: application/json;charset=UTF-8
            date: Fri, 04 Jan 2019 00:27:34 GMT
            transfer-encoding: chunked

Solution

  • your problem is with {email}.

    @GetMapping(value = "/orders/{email}")
    private List<Order> ordersByEmail(@PathVariable String email) {
    

    By specifying {email} and PathVariable, it means that your controller will take the variable after /orders/ and assign it to email.

    Try http://localhost:8084/api/v1/processor/orders/string and curl -X GET "http://localhost:8084/api/v1/processor/orders/string" -H "accept: /" instead. you do not need to explicitly enter {email} in your url