MapStruct: use custom factory method valueobject.of(

35 Views Asked by At

My related class:

package net.gencat.transversal.espaidoc.domain.model.reference;

import java.util.UUID;

import lombok.Getter;

@Getter
public class ReferenceId {

    private final UUID id;

    private ReferenceId(UUID id) {
        validate(id);
        this.id = id;
    }

    public static ReferenceId of(UUID id) {
        return new ReferenceId(id);
    }

    private static void validate(UUID id) {
        if (id == null) {
            throw new IllegalArgumentException("Value cannot be null or empty");
        }
    }
}

As you can see, my factory method is of(UUID id).

When I'm trying to create a Mapper related with ReferenceId class,I'm getting:

ReferenceId does not have an accessible constructor.

How could I instruct to mapstruct that uses of(... factory methods instead of...?

1

There are 1 best solutions below

0
PeterMmm On

AFAIK Mapstruct needs at least a public non arg c'tor or any other, annotated with @Default or a factory class.

C'tor only private cannot work, because the mapper class that Mapstruct creates must instantiate an object of the target class.

Anyway, I do not see the difference to use of() or, then public, c'tor ReferenceId(UUID id).