Skip to main content

Fetch a role with the Java SDK

permit.api.roles.get() fetches one role from your Permit environment. This reference is for Java developers who read role-based access control (RBAC) roles from code. The examples assume an initialized Permit client named permit, as set up in Check permissions with the Java SDK.

Signature

The SDK has three methods that fetch a role:

MethodSignature
get()RoleRead get(String roleKey)
getByKey()RoleRead getByKey(String roleKey)
getById()RoleRead getById(UUID roleId)

All three methods throw IOException, PermitApiError, and PermitContextError. getByKey() and getById() call get() with the key or the ID as a string.

Parameters

ParameterTypeMethodDescription
roleKeyStringget(), getByKey()The key or the ID (UUID) of the role. The Permit API accepts either value.
roleIdjava.util.UUIDgetById()The ID of the role.

Example

Replace [ROLE ID OR KEY], [ROLE KEY], and [ROLE ID] with values from your environment.

import io.permit.sdk.openapi.models.RoleRead;
import java.util.UUID;

// accepts both the role id and the role key
RoleRead role = permit.api.roles.get("[ROLE ID OR KEY]");

// if you want to be explicit, you can use getByKey
RoleRead role2 = permit.api.roles.getByKey("[ROLE KEY]");

// or getById
RoleRead role3 = permit.api.roles.getById(UUID.fromString("[ROLE ID]"));

Return value

Each method returns a RoleRead object (io.permit.sdk.openapi.models.RoleRead) with the role's key, name, ID, description, and permissions.

Exceptions

All exception classes except IOException are in the io.permit.sdk.api package.

ExceptionThrown when
IOExceptionThe HTTP request to the Permit API fails, for example because of a network error.
PermitApiErrorThe Permit API returns an error status code. If no role matches the key or ID, the API returns an error status code. getResponseCode() returns the HTTP status code and getRawResponse() returns the response body.
PermitContextErrorThe SDK context does not include an environment, for example because the API key is scoped to an organization or project.