如何在微服务应用程序中注入PlatformApi?

时间:2018-03-28 12:16:53

标签: cumulocity

我需要构建一个小型微服务,它接收REST请求并在QuarkIoE中存储数据(设备,测量等)。

对我来说,从文档中可以很清楚如何将platformApi轻松注入到我的Spring Boot应用程序中。特别是Scope(TenantScope和UserScope)的使用尚不清楚。

你能给一个非常简单的'#34; hello-world"示例如何自动装载platformApi(例如库存)和应用程序启动时在租户范围内执行某些操作(打印出所有设备)并使用RestControler RequestMapping在用户范围内执行某些操作?

以下是代码段:

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();
}
}

到目前为止我尝试的所有内容都会导致启动错误,因为bean无法注入,或者它们不在所需的范围内或不在上下文中。 我有一个application.properties,其中包含bootstrap凭据。

1 个答案:

答案 0 :(得分:0)

我找到了第一个解决方案,但我仍然不确定租户和用户范围之间的差异以及何时使用范围。但也许这有点帮助。

我无法自动装配库存API,但我能够自动装配平台API,您可以使用库存API。

package c8y.example;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cumulocity.microservice.autoconfigure.MicroserviceApplication;
import com.cumulocity.model.idtype.GId;
import com.cumulocity.rest.representation.inventory.ManagedObjectRepresentation;
import com.cumulocity.sdk.client.Platform;
import com.cumulocity.sdk.client.inventory.InventoryApi;

@MicroserviceApplication
@RestController
public class App {
    private static final Logger log = org.slf4j.LoggerFactory.getLogger(App.class);

    @Autowired(required = true)
    @Qualifier("userPlatform")
    private Platform platformApi;

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @RequestMapping("/")
    public String greeting() {
        if (platformApi != null && platformApi.getInventoryApi() != null) {
            final InventoryApi inventoryApi = platformApi.getInventoryApi();
            final ManagedObjectRepresentation managedObjectRepresentation = inventoryApi.get(new GId("5749"));

            log.info(managedObjectRepresentation.toString());
            return "device: " + managedObjectRepresentation.getName();
        }

        return "hello world";
    }
}

最初有一个可用的范围是UserScope,但是当您查看CumulocityClientFeature类时,您可以看到该类将Tenant Scope的PlatformImpl设置为Primary。这意味着当您自动装载PlatformApi时,它将自动使用TenantScoped平台而不是UserScoped。这将失败,因为当前的Scope是UserScope。要自动装配正确的PlatformApi,您必须使用值为userPlatform的限定符,以便自动装配UserScoped PlatformAPI。您可以从平台API访问广告资源API。

尝试自动装配Inventory API对我不起作用,因为应用程序无法找到UserScoped Inventory API。不知道为什么。