Java 如何在seedstack中指定默认汇编程序?

Java 如何在seedstack中指定默认汇编程序?,java,seedstack,Java,Seedstack,我使用的是19.11版本的,我想使用汇编程序将聚合列表转换为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.

我使用的是19.11版本的,我想使用汇编程序将聚合列表转换为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;
    }
}

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

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

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

谢谢!!它只需将
.with(ModelMapper.class)
添加到“assemble”方法调用中即可解决此问题。您知道为我的整个项目包指定默认汇编程序实现的方法吗?(例如,我想对org.mypackage.resources中的所有汇编器使用ModelMapper实现?)。该机制与定义defaultRepository的机制基本相同,但您必须为限定符指定defaultAssembler和target,而不是defaultRepository。在您的案例中,您必须在配置中添加类似的内容
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;
    }
}
@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;
    }
}