Create or update role mapping APIedit


Creating and updating a role mapping can be performed using the security().putRoleMapping() method:

final RoleMapperExpression rules = AnyRoleMapperExpression.builder()
final PutRoleMappingRequest request = new PutRoleMappingRequest("mapping-example", true,
    Collections.singletonList("superuser"), Collections.emptyList(), rules, null, RefreshPolicy.NONE);
final PutRoleMappingResponse response =, RequestOptions.DEFAULT);


The returned PutRoleMappingResponse contains a single field, created. This field serves as an indication if a role mapping was created or if an existing entry was updated.

boolean isCreated = response.isCreated(); 

created is a boolean indicating whether the role mapping was created or updated

Asynchronous Executionedit

This request can be executed asynchronously using the security().putRoleMappingAsync() method:, RequestOptions.DEFAULT, listener); 

The PutRoleMappingRequest to execute and the ActionListener to use when the execution completes

The asynchronous method does not block and returns immediately. Once the request has completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed.

A typical listener for a PutRoleMappingResponse looks like:

ActionListener<PutRoleMappingResponse> listener = new ActionListener<PutRoleMappingResponse>() {
    public void onResponse(PutRoleMappingResponse response) {

    public void onFailure(Exception e) {

Called when the execution is successfully completed. The response is provided as an argument

Called in case of failure. The raised exception is provided as an argument