java - 如何在种子堆栈中指定默认汇编程序?
问题描述
我正在使用 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 的类
- 包括一个为您提供默认汇编的插件(如文档中所述)
推荐阅读
- drools - 流口水比 EasyScoreCalculator [OptaPlanner] 慢得多
- javascript - 问题/错误 'Property''xs' 在带有 tensorflow.js 的 Angular 项目中的类型 'TensorContainer' 上不存在
- java - Java 作业(初学者) - 我认为我的数学或声明有问题?
- github - 如何从 PC 中永久删除旧的 github 帐户
- jsf - 为什么单独的 RequestScoped bean 没有注入到 JSF commandButton 动作调用的 bean 中?
- android - 如何支持 sdk 28+ 的 64 位架构?
- python - + 不支持的操作数类型:'float' 和 'datetime.timedelta'", '发生在索引 5')
- c# - 尝试访问文件
- macos - 在安装 SMB 驱动器并在没有 root 的情况下读取它时遇到问题
- html - 如何在不拉伸的情况下将图像放入css-grid