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

**URL:** <https://community.cumulocity.com/t/switching-default-context-in-microservice-sdk-from-service-user-to-user-context/7673>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [July 20, 2023, 3:34pm UTC](https://community.cumulocity.com/t/switching-default-context-in-microservice-sdk-from-service-user-to-user-context/7673 "2023-07-20T15:34:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lavanya\_Ramesh](https://avatars.discourse-cdn.com/v4/letter/l/85f322/32.png) [@Lavanya\_Ramesh](https://community.cumulocity.com/u/Lavanya_Ramesh)\
**Post date:** [July 20, 2023, 3:34pm UTC](https://community.cumulocity.com/t/switching-default-context-in-microservice-sdk-from-service-user-to-user-context/7673/1 "2023-07-20T15:34:45Z")

</div>

#### _ **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](https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcumulocity.com%2Fguides%2Fmicroservice-sdk%2Fjava%2F%23context-support&data=05%7C01%7CLavanya.Ramesh%40softwareag.com%7C92e934969ed64b1b3be908db88f658c7%7Cd9662eb9ad984e74a8a204ed5d544db6%7C1%7C0%7C638254363876586938%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=c%2FVX0aDeUvb1rHPgwcu65fw8FrH72Lui5hAcuVah4OI%3D&reserved=0)?
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.

```auto
@UserScope
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?_

Development

---

<div class="post-metadata">

**Author:** ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)\
**Post date:** [July 20, 2023, 3:54pm UTC](https://community.cumulocity.com/t/switching-default-context-in-microservice-sdk-from-service-user-to-user-context/7673/2 "2023-07-20T15:54:51Z")

</div>

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

```auto
    @Autowired
    @Qualifier("userInventoryApi")
    InventoryApi userInventoryApi;

```

1. You can switch using different context by using different beans:

```auto
    @Autowired
    @Qualifier("userInventoryApi")
    InventoryApi userInventoryApi;

    @Autowired
    InventoryApi tenantInventoryApi;

```

1. see 1 and 2.

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

> **[cumulocity-microservice-templates/context at main ·...](https://github.com/Cumulocity-IoT/cumulocity-microservice-templates/tree/main/context)**
>
> Collection of templates / examples to give the developer a starting point how common features can be solved by using Cumulocity Microservice SDK for Java. Developed by Global Competency Center IoT...

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

1. UserPlatform
2. UserBean
3. TenantBean

RESTController:

> <https://github.com/Cumulocity-IoT/cumulocity-microservice-templates/blob/main/context/src/main/java/com/c8y/ms/templates/context/controller/DeviceController.java>

DeviceService:

> <https://github.com/Cumulocity-IoT/cumulocity-microservice-templates/blob/main/context/src/main/java/com/c8y/ms/templates/context/service/DeviceService.java>

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [January 16, 2024, 3:55pm UTC](https://community.cumulocity.com/t/switching-default-context-in-microservice-sdk-from-service-user-to-user-context/7673/3 "2024-01-16T15:55:31Z")

</div>

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