I have a spring mvc API (config in XML) with multiple services inside. But when I try to add a service with a @RequestMapping
containing a path variable /{theCurrencyCode}
, the resource created is not what I expected.
What I expected :
http://localhost:8080/api/v3/parameters/currencies/EUR
What works :
http://localhost:8080/api/v3/parameters/currencies/{theCurrencyCode}?theCurrencyCode=EUR
This is my mapping :
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@RequestMapping(value = "v3/", produces = { APPLICATION_JSON_VALUE })
public interface ParametersApi {
@RequestMapping(
value = "/parameters/currencies/{theCurrencyCode}",
produces = { "application/json" },
method = RequestMethod.GET)
ResponseEntity<List<Currency>> GetCurrencies(@PathVariable("theCurrencyCode") String theCurrencyCode);
}
Implementation :
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import java.util.ArrayList;
import java.util.List;
@Controller
public class ParametersApiController implements ParametersApi{
private final CurrenciesService service;
@Autowired
public ParametersApiController(CurrenciesService service) {
this.service = service;
}
@Override
public ResponseEntity<List<Currency>> GetCurrencies(String code) {
final List<Currency> currencies = service.getCurrencies(code);
return new ResponseEntity<>(currencies, HttpStatus.OK);
}
}
Swagger UI confirmed this, seeing theCurrencyCode
as a "Parameter Type" query
instead of path
.
How do I get my @PathVariable to work ?