无法从我的Spring Boot应用的.env文件中检测到API密钥。

huangapple go评论91阅读模式
英文:

Cannot detect API key from .env in my Spring Boot App

问题

I want to create an app to use API Layer for currency exchange. I used an API key of Exchange Rates Data API on apilayer.com.

When I send a request to Get Rates, defined in the postman_collection in my repo, I get this error message shown below:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "message" (class com.exchangeapi.currencyexchange.exception.RestTemplateError), not marked as ignorable (4 known properties: "error", "status", "path", "timestamp"])
 at [Source: (String)"{"message":"No API key found in request"}"; line: 1, column: 42] (through reference chain: com.exchangeapi.currencyexchange.exception.RestTemplateError["message"])

It always detects it as default-key.

After the endpoint converts to this URL, like https://api.apilayer.com/exchangerates_data/latest?symbols=USD%2CTRY&base=EUR, I revised it in the getExchangeUrl method.

How can I fix it?

Here is the .env file shown below (I defined .gitignore not to upload to git repo):

EXCHANGE_API_API_KEY=MY_APIKEY

Here is the application.yml:

exchange-api:
  api-url: https://api.apilayer.com/exchangerates_data/
  api-key: ${EXCHANGE_API_API_KEY:default-key}
  api-call-limit: 60
  cache-name: exchanges
  cache-ttl: 10000

Here is the Constants class shown below:

@Component
public class Constants {

    public static String EXCHANGE_API_BASE_URL;
    public static String EXCHANGE_API_API_KEY;

    public static String EXCHANGE_CACHE_NAME;
    public static Integer EXCHANGE_API_CALL_LIMIT;

    @Value("${exchange-api.api-url}")
    public void setExchangeApiBaseUrl(String apiUrl) {
        Constants.EXCHANGE_API_BASE_URL = apiUrl;
    }

    @Value("${exchange-api.api-key}")
    public void setExchangeApiKey(String apiKey) {
        EXCHANGE_API_API_KEY = apiKey;
    }

    @Value("${exchange-api.cache-name}")
    public void setExchangeCacheName(String cacheName) {
        Constants.EXCHANGE_CACHE_NAME = cacheName;
    }

    @Value("${exchange-api.api-call-limit}")
    public void setExchangeApiCallLimit(Integer apiCallLimit) {
        EXCHANGE_API_CALL_LIMIT = apiCallLimit;
    }

}

Here are the relevant methods from the saveRatesFromApi of RateService:

private RateEntity saveRatesFromApi(LocalDate rateDate, EnumCurrency base, List<EnumCurrency> targets) {

        log.info("ExchangeService | saveRatesFromApi is called");

        HttpHeaders headers = new HttpHeaders();
        headers.add("apikey", EXCHANGE_API_API_KEY);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        final HttpEntity<String> headersEntity = new HttpEntity<>(headers);
        String url = getExchangeUrl(rateDate, base, targets);

        ResponseEntity<RateResponse> responseEntity = restTemplate.exchange(url, HttpMethod.GET, headersEntity, RateResponse.class);

        RateResponse rates = responseEntity.getBody();
        RateEntity entity = convert(rates);
        entity.setDate(rateDate);
        return rateRepository.save(entity);
    }

private String getExchangeUrl(LocalDate rateDate, EnumCurrency base, List<EnumCurrency> targets) {
        String symbols = String.join("%2C", targets.stream().map(EnumCurrency::name).toArray(String[]::new));
        return EXCHANGE_API_BASE_URL + rateDate + "?symbols=" + symbols + "&base=" + base;
    }

Here is the repo: Link

英文:

I want to create an app to use API Layer for currency exchange. I used a API key of Exchange Rates Data API in apilayer.com.

When I send a request to Get Rates defined in postman_collection in my repo.

I get this error message shown below

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field &quot;message&quot; (class com.exchangeapi.currencyexchange.exception.RestTemplateError), not marked as ignorable (4 known properties: &quot;error&quot;, &quot;status&quot;, &quot;path&quot;, &quot;timestamp&quot;])
 at [Source: (String)&quot;{&quot;message&quot;:&quot;No API key found in request&quot;}&quot;; line: 1, column: 42] (through reference chain: com.exchangeapi.currencyexchange.exception.RestTemplateError[&quot;message&quot;])

It always detect it as default-key.

After endpoint converts to this URL like https://api.apilayer.com/exchangerates_data/latest?symbols=USD%2CTRY&amp;base=EUR, I revised it in getExchangeUrl method.

How can I fix it?

Here is the .env file shown below (I defined .gitignore not to upload to git repo)

EXCHANGE_API_API_KEY=MY_APIKEY

Here is the application.yml

exchange-api:
  api-url: https://api.apilayer.com/exchangerates_data/
  api-key: ${EXCHANGE_API_API_KEY:default-key}
  api-call-limit: 60
  cache-name: exchanges
  cache-ttl: 10000

Here is the Constants class shown below

@Component
public class Constants {

    public static String EXCHANGE_API_BASE_URL;
    public static String EXCHANGE_API_API_KEY;

    public static String EXCHANGE_CACHE_NAME;
    public static Integer EXCHANGE_API_CALL_LIMIT;

    @Value(&quot;${exchange-api.api-url}&quot;)
    public void setExchangeApiBaseUrl(String apiUrl) {
        Constants.EXCHANGE_API_BASE_URL = apiUrl;
    }

    @Value(&quot;${exchange-api.api-key}&quot;)
    public void setExchangeApiKey(String apiKey) {
        EXCHANGE_API_API_KEY = apiKey;
    }

    @Value(&quot;${exchange-api.cache-name}&quot;)
    public void setExchangeCacheName(String cacheName) {
        Constants.EXCHANGE_CACHE_NAME = cacheName;
    }

    @Value(&quot;${exchange-api.api-call-limit}&quot;)
    public void setExchangeApiCallLimit(Integer apiCallLimit) {
        EXCHANGE_API_CALL_LIMIT = apiCallLimit;
    }

}

Here is the relevant methods saveRatesFromApi of RateService

private RateEntity saveRatesFromApi(LocalDate rateDate, EnumCurrency base, List&lt;EnumCurrency&gt; targets) {

        log.info(&quot;ExchangeService | saveRatesFromApi is called&quot;);

        HttpHeaders headers = new HttpHeaders();
        headers.add(&quot;apikey&quot;, EXCHANGE_API_API_KEY);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        final HttpEntity&lt;String&gt; headersEntity = new HttpEntity&lt;&gt;(headers);
        String url = getExchangeUrl(rateDate, base, targets);

        ResponseEntity&lt;RateResponse&gt; responseEntity = restTemplate.exchange(url, HttpMethod.GET, headersEntity, RateResponse.class);

        RateResponse rates = responseEntity.getBody();
        RateEntity entity = convert(rates);
        entity.setDate(rateDate);
        return rateRepository.save(entity);
    }

private String getExchangeUrl(LocalDate rateDate, EnumCurrency base, List&lt;EnumCurrency&gt; targets) {
        String symbols = String.join(&quot;%2C&quot;, targets.stream().map(EnumCurrency::name).toArray(String[]::new));
        return EXCHANGE_API_BASE_URL + rateDate + &quot;?symbols=&quot; + symbols + &quot;&amp;base=&quot; + base;
    }

Here is the repo : Link

答案1

得分: 0

我解决了这个问题。

在我在application.yml文件中定义了这部分之后,问题就消失了。

spring:
config:
import: optional:file:.env[.properties]

英文:

I solved the issue.

After I defined this part in the application.yml file, the issue was disappeared.

spring:
  config:
    import: optional:file:.env[.properties]

huangapple
  • 本文由 发表于 2023年5月21日 22:28:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定