Spring Boot微服务中PlatformApi注入与作用域使用方法及示例请求
Got it, let's break this down into a minimal, working "hello-world" example that shows you how to auto-wire Cumulocity's PlatformApi components and run tenant-scoped operations on app startup. I'll cover all the key parts you're confused about: dependencies, configuration, and the actual code to list devices.
Step 1: Add Cumulocity Spring Boot Starter Dependency
First, include the official Cumulocity Spring Boot starter in your pom.xml (for Maven) — this handles all auto-configuration for PlatformApi components so you don't have to wire everything manually:
<dependency> <groupId>com.cumulocity.sdk</groupId> <artifactId>cumulocity-spring-boot-starter</artifactId> <version>10.18.0</version> <!-- Use the latest stable version available --> </dependency>
Step 2: Configure Cumulocity Connection
Add these properties to your application.properties file to connect to your Cumulocity instance. Use a service user (a tenant-level account with appropriate permissions) here:
# Cumulocity instance connection details cumulocity.base-url=https://your-cumulocity-tenant.com cumulocity.tenant=your-tenant-id cumulocity.username=your-service-username cumulocity.password=your-service-password
Step 3: Tenant-Scoped Device Listing on Startup
Create a component that runs automatically when your app starts, injects the InventoryApi (part of PlatformApi), and uses TenantContextHolder to enforce tenant scope. We'll use Spring's ApplicationRunner to trigger this code on startup:
import com.cumulocity.sdk.client.inventory.InventoryApi; import com.cumulocity.sdk.client.inventory.InventoryFilter; import com.cumulocity.sdk.client.inventory.ManagedObjectCollection; import com.cumulocity.sdk.client.inventory.ManagedObjectRepresentation; import com.cumulocity.sdk.client.tenant.TenantContextHolder; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component public class TenantScopedDeviceLister implements ApplicationRunner { private final InventoryApi inventoryApi; // Constructor injection (preferred over @Autowired for clarity and testability) public TenantScopedDeviceLister(InventoryApi inventoryApi) { this.inventoryApi = inventoryApi; } @Override public void run(ApplicationArguments args) throws Exception { // Explicitly set the tenant context — the starter may auto-set this, but being explicit avoids confusion TenantContextHolder.setCurrentTenant("your-tenant-id"); // Filter to only fetch objects marked as devices (using Cumulocity's standard device fragment) InventoryFilter deviceFilter = new InventoryFilter(); deviceFilter.withFragmentType("c8y_IsDevice"); // Fetch all devices in the tenant ManagedObjectCollection devices = inventoryApi.getManagedObjects().get(deviceFilter); // Print device details to the console System.out.println("=== All Devices in Tenant ==="); for (ManagedObjectRepresentation device : devices.allPages()) { System.out.printf("Device ID: %s | Name: %s%n", device.getId(), device.getName()); } } }
Key Clarification: TenantScope vs UserScope
- TenantScope: Used for operations that apply to the entire tenant (like listing all devices). You set this with
TenantContextHolder.setCurrentTenant(), and API calls use the tenant-level permissions of your service user. This is the right choice for backend services that need full access to tenant data. - UserScope: Used for operations restricted to a specific end-user's permissions (e.g., listing only devices a user is allowed to view). You'd use
UserContextHolder.setCurrentUser()here, but this is less common for backend microservices.
Quick Permission Check
Make sure your service user has the ROLE_INVENTORY_READ role assigned in Cumulocity — without this, you'll get permission denied errors when trying to fetch devices.
That's all! When you start your Spring Boot app, it will connect to Cumulocity, switch to your tenant's scope, and print all devices in the console automatically.
内容的提问来源于stack exchange,提问作者Switschel

