首页 > 解决方案 > 如何在种子堆栈中指定默认汇编程序?

问题描述

我正在使用 19.11 版本的seedstack,我想使用FluentAssembler汇编器将聚合列表转换为 DTO 列表。

调用 fluentAssembler.assemble 方法时出现以下错误:

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.

我不知道如何指定限定符,我想使用默认模型映射器...

这是资源代码:

@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","edward.teach@pirates.org"));
        customerList.add(buildCustomer("006","Olivier Levasseur","olivier.levasseur@pirates.org"));
        customerList.add(buildCustomer("007","James Bond","james.bond@mi6.uk"));
        return customerList;
    }

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

聚合:

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;
    }
}

和 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;
    }
}

标签: javaseedstack

解决方案


FluentAssembler 只负责将 Dto 与 Assembler 匹配,但不提供 Assembler 本身的默认实现。

您有 2 个选项来提供默认汇编程序。

  • 构建一个实现 Asselmber 的类
  • 包括一个为您提供默认汇编的插件(如文档中所述)

推荐阅读