我使用了 Lombok、Open Feign 和 Spring Web
我有 currencyClient 介面:
@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);
}
和控制器:
@RestController
@RequiredArgsConstructor
public class StatusController {
private String appId1 = "appId";
private final currencyClient currencyClient;
@GetMapping("/getAllCurrency")
public List<Object> getCurrency(){
return currencyClient.getCurrency(appId1);
}
}
并且“ http://localhost:1212/getAllCurrency ”不起作用,因為鏈接被轉換為“** https://openexchangerates.org/api/historical/2012-07-10.json/appId**”我明白了那 &/= 被顛倒了,我也認為我對 List 的指示是不正確的。這就是我嘗試過的,所以我如何從“** https://openexchangerates.org/api/historical/2012-07-10.json?app_id**”作為“ http://localhost:1212/getAllCurrency ”獲取資訊?
uj5u.com熱心網友回復:
根據https://docs.openexchangerates.org檔案,app_id應該是請求引數(請參閱 參考資料@RequestParam),而不是路徑變數。你可以這樣做:
CurrencyClient界面:
@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);
}
}
這里還有一些需要注意的事項:
請不要將您的 API 密鑰發布到 StackOverflow 或其他任何公開的地方。其他人可能會濫用它。既然您已經發布了它,您應該請求一個新的 API 密鑰并擺脫這個(如果可能的話關閉它)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/485707.html
上一篇:SpringSecurityOauth2Client是否自動從SpringAuthorizationServer處理重繪令牌?
