How to specify a default assembler in seedstack?

Sebastien Moneuse :

I'm using the 19.11 version of seedstack, and I want to use the FluentAssembler assembler to convert an aggregate List into a DTO List.

I'm getting the following error when I call the fluentAssembler.assemble method :

org.seedstack.business.internal.BusinessException: [BUSINESS] Unable to find assembler

Description
-----------
No assembler was found to assemble 'com.inetpsa.svr.domain.model.customer.Customer(Customer.java:1)' to
'com.inetpsa.svr.interfaces.rest.customer.CustomerRepresentation(CustomerRepresentation.java:1)'.

Fix
---
Make sure that an assembler without qualifier exists. If you want to use a qualified assembler (like a default
assembler), specify its qualifier.

I don't know howto specify the qualifier, I'd like to use a default model mapper...

Here is The Resource code :

@Path("customers")
public class CustomerResource {

    @Inject
    private FluentAssembler fluentAssembler;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<CustomerRepresentation> listAllCustomers() {
        List<Customer> customerList = fetchAllCustomers();
        return fluentAssembler.assemble(customerList).toListOf(CustomerRepresentation.class);
    }

    /**
     * Test method - Should be replaced by a repository
     * @return List<Customer> all customers
     */
    private List<Customer> fetchAllCustomers(){
        List<Customer> customerList = new ArrayList<>();
        customerList.add(buildCustomer("005","Edward Teach","[email protected]"));
        customerList.add(buildCustomer("006","Olivier Levasseur","[email protected]"));
        customerList.add(buildCustomer("007","James Bond","[email protected]"));
        return customerList;
    }

    private Customer buildCustomer(String id, String name, String mail){
        Customer result = new Customer(id);
        result.updateNameAndMail(name, mail);
        return result;
    }
}

The aggregate :

public class Customer extends BaseAggregateRoot<String> {

    @Identity
    private String identifier;
    private String name;
    private String mail;

    public Customer(String identifier){
        this.identifier=identifier;
    }

    public void updateNameAndMail(String name, String mail){
        if(StringUtils.isBlank(name)){
            throw new IllegalArgumentException("Name can't be blank");
        }
        if(StringUtils.isBlank(mail)){
            throw new IllegalArgumentException("Mail can't be blank");
        }
        this.name=name;
        this.mail=mail;
    }

    public String getIdentifier() {
        return identifier;
    }

    public String getName() {
        return name;
    }

    public String getMail() {
        return mail;
    }
}

And the DTO :

@DtoOf(Customer.class)
public class CustomerRepresentation {
    private String identifier;
    private String name;
    private String mail;

    /**
     * Required public no parameters constructor
     */
    public CustomerRepresentation(){}

    public CustomerRepresentation(String identifier, String name, String mail){

    }
    @AggregateId
    public String getIdentifier() {
        return identifier;
    }

    public String getMail() {
        return mail;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }
}
Sherpard :

FluentAssembler only takes care of matching a Dto with an Assembler, but does not provide a default implementation of an Assembler by itself.

You have 2 Options to provide a Default Assembler.

  • Build a class that implements Asselmber
  • Include an addon that provides that Default Assember for you (As stated on the docs)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=422976&siteId=1