REST

This module can expose REST APIs for doing CRUD operations on aggregates, through their DTO.

Dependency

Enabling automatic REST publication of CRUD operations requires the following dependency in your project:

<dependency>
    <groupId>org.seedstack.addons.crud</groupId>
    <artifactId>crud-rest</artifactId>
</dependency>
Show version
dependencies {
    compile("org.seedstack.addons.crud:crud-rest:1.0.1")
}

Usage

To be exposed as a CRUD REST API, the DTO:

  • Must be annotated with @DtoOf, linking it to its corresponding aggregate.
  • Must have a default or a custom assembler that can handle the mapping to its aggregate and back.
  • Must be properly annotated so the fluent assembler DSL can do the mapping automatically.

As an example consider the class CustomerDto below, corresponding to the Customer aggregate and annotated so the fluent assembler can do the mapping automatically:

@DtoOf(Customer.class)
public class CustomerDto {
    private String id;
    
    @AggregateId
    @FactoryArgument
    private String getId() {
        return id;
    }
    
    private void setId(String id) {
        this.id = id;
    }
}

If you’re not familiar with business framework assemblers and its fluent assembler DSL, read this documentation.

Default resource

To generate a default REST resource exposing CRUD operations for a DTO, annotate the DTO with @RestCrud:

@DtoOf(Customer.class)
@RestCrud("/customers")
public class CustomerDto {
    private String id;
    
    @AggregateId
    @FactoryArgument
    private String getId() {
        return id;
    }
    
    private void setId(String id) {
        this.id = id;
    }
}

You can control which CRUD operation(s) are exposed by using the corresponding @RestCrud boolean parameters.

Custom resource

To customize the default CRUD behavior, you have to create a resource class extending BaseResource:

@Path("/customers")
public class SomeResource extends BaseResource {
    
}

Solely extending BaseResource, by itself, will not provide any behavior. You have two choices, described below, to add CRUD operation(s) to your resource class.

Interfaces

You can implement any combination of the following four CRUD interfaces: CreateResource, ReadResource, UpdateResource and DeleteResource:

@Path("/customers")
public class SomeResource extends BaseResource implements ReadResource {
    // Override inherited operations or add custom operations here 
}

Each interface provides the behavior for the corresponding operation. You can then augment or override this behavior with your own operations.

Base class

You can extend the BaseCrudResource class instead, which in turn implements all four interfaces described in the section above:

@Path("/customers")
public class SomeResource extends BaseCrudResource {
    // Override inherited operations or add custom operations here 
}

The base class provides the behavior for the all CRUD operations. You can then augment or override this behavior with your own operations.

   

On this page


Edit this page