Get Application ID or Managed Object ID of Cumulocity Microservice

For those who want to address the “current application” REST endpoint via Java (like me), here is a lean implementation utilizing the Cumulocity Java SDK:

import org.springframework.beans.factory.annotation.Autowired;

import com.cumulocity.microservice.context.ContextService;
import com.cumulocity.microservice.context.credentials.MicroserviceCredentials;
import com.cumulocity.microservice.subscription.model.core.PlatformProperties;
import com.cumulocity.microservice.subscription.repository.application.ApplicationApi;
import com.cumulocity.rest.representation.application.ApplicationRepresentation;

@Autowired
private ContextService<MicroserviceCredentials> contextService;
@Autowired
private PlatformProperties platformProperties;
@Autowired
private ApplicationApi appApi;

public ApplicationRepresentation getCurrentApplication() {
	return contextService.callWithinContext((MicroserviceCredentials)platformProperties.getMicroserviceBoostrapUser(), () -> {
		return appApi.currentApplication().get();
	});
}

The ApplicationAPI class wraps the relevant REST interface. The other pieces of the code are used for accessing the endpoint with the right permissions.

3 Likes