I have a properties.yml file , with the information shown below:
clave:
location: "ap"
apiKey: "QmZ6LvNqzsSSgTTLj1k7ncbaVRumlJ5XgBvadf_7MQaH"
the information is called from a controller, which has the following information:
@RestController
@RequestMapping("/file")
public class StorageController {
@Autowired
StorageConfig storageConfig;
@Autowired
StorageService storageService;
@Value("${clave.apiKey}")
private String apiKey;
@Value("${clave.serviceInstanceId}")
private String serviceInstanceId;
@Value("${clave.endpointUrl}")
private String endpointUrl;
@Value("${clave.location}")
private String location;
AmazonS3 client = storageConfig.createClient(apiKey, "crn:v1:bluemix:public:cloud-object-storage:global:a/ef304b2c20b5447eac59a8bb3e85812c:15494859-a02d-45d7-9803-e24c7dfcd5fc::", "https://s3.ap.cloud-object-storage.appdomain.cloud", location);
@PostMapping("/upload") // //new annotation since 4.3
public String singleFileUpload(@RequestParam("file") MultipartFile file) {
try {
storageService.uploadFile(client, file);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/uploadStatus";
}
}
And it happens that I get an error when I call the apiKey, which I have created in properties. Whereas when I make the location call, it doesn't give me a problem in the createClient method.
Do you know if I'm making a mistake when calling the data? o Is there another way to make the call? I appreciate the help.
I'm not sure why it's working with 'location', in both cases it should fail.
It so happens that you are trying to introduce logic (execute the createClient method) in the dependency resolution stage of Springboot. At this stage the apiKey and Location values have not been assigned because @Value is a Runtime tag. In other words, at the point where they are used by the method, both variables are null.
You may:
Execute that createCliend logic inside the singleFileUpload method and it will take both values
Inject the dependency by constructor and not by parameter, something like:
This is what I interpret that may be happening, however it would be better if you edit the question and include the error that you get, so the answers can be a little more accurate to what you need.