Home > Blockchain >  How can I get api with FeignClient
How can I get api with FeignClient

Time:06-05

I used Lombok, Open Feign and Spring Web

I have currencyClient interface:

@FeignClient(value = "getcurrency", url = "https://openexchangerates.org")
public interface currencyClient {

@RequestMapping(value = "/api/historical/2012-07-10.json/{smt}", method = RequestMethod.GET)
public List<Object> getCurrency(@PathVariable String smt);
}

And Controller:

@RestController
@RequiredArgsConstructor
public class StatusController {

private String appId1 = "appId";
private final currencyClient currencyClient;

@GetMapping("/getAllCurrency")
public List<Object> getCurrency(){
    return currencyClient.getCurrency(appId1);
}

}

And "http://localhost:1212/getAllCurrency" is not working cause the link is converted into "**https://openexchangerates.org/api/historical/2012-07-10.json/appId**" I understand that &/= are reversed and I also think that my indication of List is not correct. That's what I tried so how can I get info from "**https://openexchangerates.org/api/historical/2012-07-10.json?app_id**" as "http://localhost:1212/getAllCurrency"?

CodePudding user response:

According to the https://docs.openexchangerates.org docs, the app_id should be a request parameter (see @RequestParam), not a path variable. You could do something like this:

CurrencyClient interface:

@FeignClient(value = "getcurrency", url = "https://openexchangerates.org")
public interface CurrencyClient {

    @RequestMapping(value = "/api/historical/2012-07-10.json", method = RequestMethod.GET)
    Map<String, Object> getCurrency(@RequestParam("app_id") String appId);
}

StatusController:

@RestController
public class StatusController {
    private final CurrencyClient currencyClient;

    public MyController(CurrencyClient currencyClient) {
        this.currencyClient = currencyClient;
    }

    @GetMapping("/getAllCurrency")
    public Map<String, Object> getCurrency() {
        String appId1 = "*****";
        return currencyClient.getCurrency(appId1);
    }
}

Some additional things to note here:

Please don't post yout API key to StackOverflow, or anywhere else publicly. Other people might abuse it. Since you already posted it, you should request a new API key and get rid of this one (close it if possible).

  • Related