簡體   English   中英

如何在種子堆棧中指定默認匯編程序?

[英]How to specify a default assembler in seedstack?

我使用的是 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;
    }
}

FluentAssembler 只負責將 Dto 與 Assembler 進行匹配,但本身不提供 Assembler 的默認實現。

您有 2 個選項來提供默認匯編程序。

  • 構建一個實現 Asselmber 的類
  • 包括一個為您提供默認匯編程序的插件(如文檔中所述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM