这是我的第一个问题,所以我很抱歉,如果它不够具体(请温和哈哈),我已经看过文件,但我是新手,没有任何帮助。
我正在做一个简单的项目,它使用Java 18和Spring来调用外部API调用,并且我想隐藏我出于显而易见的原因使用的API键。
到目前为止,我在我的资源目录中有一个应用程序-dev.properties文件,其中包含以下内容(我正在为安全性提供实际的密钥),并且在我的.gitignore文件中有一个application-dev.properties,因此它不会被提交:
api-key=someText
我试图在我的控制器中使用这个值,比如:
@RestController
public class ImageController {
@Value("${api-key}")
private String API_KEY;
@RequestMapping(value = "/image")
public List<String> getImages(@RequestParam(defaultValue = "4") String request) {
String url = "https://api.nasa.gov/planetary/apod?" + API_KEY + request;
RestTemplate restTemplate = new RestTemplate();
我收到的错误是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'imageController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'api-key' in value "${api-key}"
提前感谢!
https://stackoverflow.com/questions/73831369
复制相似问题