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:
| Method | Signature |
|---|---|
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
| Parameter | Type | Method | Description |
|---|---|---|---|
roleKey | String | get(), getByKey() | The key or the ID (UUID) of the role. The Permit API accepts either value. |
roleId | java.util.UUID | getById() | 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.
| Exception | Thrown when |
|---|---|
IOException | The HTTP request to the Permit API fails, for example because of a network error. |
PermitApiError | The 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. |
PermitContextError | The SDK context does not include an environment, for example because the API key is scoped to an organization or project. |