I need to build a small Microservice which receives a REST Request and stores data (Devices, Measurements etc.) in Cumulocity.
For me it is not very clear from the documentation how the platformApi can be easily injected to my Spring Boot Application. Especially the Scope (TenantScope & UserScope) usage is not clear.
Can you give a very simple "hello-world" example how to autowire the platformApi (e.g. inventory) and on application startup doing something (print out all devices) within the tenant scope and using a RestControler RequestMapping doing something within a user scope?
Here is a Code Snippet:
package c8y.example;
import com.cumulocity.microservice.autoconfigure.MicroserviceApplication;
import com.cumulocity.microservice.context.inject.TenantScope;
import com.cumulocity.sdk.client.inventory.InventoryApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
@MicroserviceApplication
@RestController
public class App{
@Autowired
InventoryApi inventoryApi;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@PostConstruct
public void init() {
System.out.println("Devices: " + inventoryApi.getManagedObjects());
}
@RequestMapping("devices")
public String greeting() {
return "Devices: " + inventoryApi.getManagedObjects();
}
}
Everything I tried so far result in error on startup that the beans couldn't been injected or that they are not in the required scope or not within context. I have a application.properties which contains the bootstrap credentials.