Switching default context in Microservice SDK: From Service User to User Context

Product/components used and version/fix level:

Cumulocity IoT Microservice SDK

Detailed explanation of the problem:

I see that service user context (TenantScope) is the default when using the Microservice SDK. I would like to switch this behaviour to user context (UserScope).

  1. Can I achieve this using a configuration or do I have to manually create the related bean like suggested here: https://cumulocity.com/guides/microservice-sdk/java/#context-support?
  2. Is there a way to switch between contexts easily. For example, few calls we want to make in user context and few using tenant context?
  3. I tried the following and couldn’t get the context to work as expected. Kindly help me with a reference I can follow to get this working.
public InventoryApi inventoryApi (Platform platform) throws SDKException{
return platform.getInventoryApi();

public ManagedObjectRepresentation createMO(ManagedObjectRepresentation mor, UserCredentials uc) {

return userContextService.callWithinContext(uc, new Callable<ManagedObjectRepresentation>() {

public ManagedObjectRepresentation call() {
return inventoryApi.create(mor);

Error messages / full error message screenshot / log file:

Question related to a free trial, or to a production (customer) instance?


1 Like

Hi @Lavanya_Ramesh

I’ll try to answer your questions:

  1. To switch to the user context you need an endpoint where an user authenticates himself. If that is given you can initiate the bean in user context the following
    InventoryApi userInventoryApi;
  1. You can switch using different context by using different beans:
    InventoryApi userInventoryApi;

    InventoryApi tenantInventoryApi;
  1. see 1 and 2.

You can find a full example here also with the mentioned user beans you can initialize.

Here is a RESTController using all possible things in a DeviceService:

  1. UserPlatform
  2. UserBean
  3. TenantBean



This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.