Skip to content

ResourceSupport<T>, ResourceCollectionSupport<T> #9

@dherges

Description

@dherges

Here is an example how to add HAL resource support to an existing application where you already have a large set of domain entities.

The trick is to use a @JsonUnwrapped around the existing entity. You have a large set of existing classes that you don't want to / or cannot modify. Then, with ResourceSupport and ResourceCollectionSupport it becomes easy to add the findAll(), findOne(ID id) features.

The Supporting Cast

@Resource
public abstract class ResourceSupport<T> {

    @Link
    public final HALLink self;

    @JsonUnwrapped
    public final T domain;

    public ResourceSupport(Link self, T domain) {
        this.self = self;
        this.domain = domain;
    }
}
@Resource
public abstract class ResourceCollectionSupport<T> {

    @Link
    public final HALLink self;

    @EmbeddedResource
    public final Collection<ResourceSupport<T>> content;

    public ResourceCollectionSupport(Link self, Collection<ResourceSupport<T>> domain) {
        this.self = self;
        this.content = domain;
    }
}

The Boilerplate

    public static class Foo {
        public UUID id = UUID.randomUUID();
        public String foo = "bar";
    }

    public static class Bar {
        public BigInteger bar = BigInteger.ZERO;
    }

    public static class FooResourceSupport extends ResourceSupport<Foo> {

        public FooResourceSupport(Foo foo) {
            super(new Link.Builder("/foo/" + foo.id.toString()).build(), foo);
        }
    }

    public static class BarResourceSupport extends ResourceSupport<Bar> {

        public BarResourceSupport(Bar bar) {
            super(new Link.Builder("/bar/" + bar.bar).build(), bar);
        }

        @EmbeddedResource(value = "thisIsTheDimensionsObject")
        public FooResourceSupport dimensions;
    }

    public static class FooCollectionResource extends ResourceCollectionSupport<Foo> {

        public FooCollectionResource(Collection<Foo> domain) {
            super(new Link.Builder("/foo").build(), domain.stream().map(FooResourceSupport::new).collect(Collectors.toList()));
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions