我的映射配置有问题。每当我通过 ID 收到订单时,我都没有遇到任何问题。然而,一旦我尝试使用entityListToResponseModelList获取每个订单,每个ID(无论是否聚合)都会在邮递员中返回null。我的映射似乎是正确的,但我收到一条警告,告诉我我已经“映射了目标属性”。这是完整的警告:
#13 16.36 /usr/src/app/src/main/java/com/example/clothingstore/ordersubdomain/mapperlayer/OrderResponseMapper.java:63: warning: Unmapped target properties: "orderId, productId, customerId, employeeId". Mapping from Collection element "Order order" to "OrderResponseModel orderResponseModel".
#13 16.36 List<OrderResponseModel> entityListToResponseModelList(List<Order> orders);
^
我的响应映射器:
package com.example.clothingstore.ordersubdomain.mapperlayer;
import com.example.clothingstore.customersubdomain.datalayer.Customer;
import com.example.clothingstore.employeesubdomain.datalayer.Employee;
import com.example.clothingstore.ordersubdomain.datalayer.Order;
import com.example.clothingstore.ordersubdomain.presentationlayer.OrderController;
import com.example.clothingstore.ordersubdomain.presentationlayer.OrderResponseModel;
import com.example.clothingstore.productsubdomain.datalayer.Product;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.springframework.hateoas.Link;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@Mapper(componentModel = "spring")
public interface OrderResponseMapper {
@Mapping(expression = "java(order.getOrderIdentifier().getOrderId())", target = "orderId")
@Mapping(expression = "java(order.getProductIdentifier().getProductId())", target = "productId")
@Mapping(expression = "java(product.getName())", target = "name")
@Mapping(expression = "java(product.getPrice())", target = "price")
@Mapping(expression = "java(order.getCustomerIdentifier().getCustomerId())", target = "customerId")
@Mapping(expression = "java(customer.getBillingAddress().getStreetAddress())", target = "streetAddress")
@Mapping(expression = "java(customer.getBillingAddress().getPostalCode())", target = "postalCode")
@Mapping(expression = "java(customer.getBillingAddress().getCity())", target = "city")
@Mapping(expression = "java(customer.getBillingAddress().getProvince())", target = "province")
@Mapping(expression = "java(order.getEmployeeIdentifier().getEmployeeId())", target = "employeeId")
@Mapping(expression = "java(employee.getLastName())", target = "lastName")
@Mapping(expression = "java(order.getDeliveryStatus().name())", target = "deliveryStatus")
@Mapping(expression = "java(order.getShippingPrice())", target = "shippingPrice")
@Mapping(expression = "java(order.getTotalPrice())", target = "totalPrice")
OrderResponseModel entityToResponseModel(Order order, Product product, Customer customer, Employee employee);
@AfterMapping
default void addLinks(@MappingTarget OrderResponseModel model){
Link selfLink = linkTo(methodOn(OrderController.class)
.getOrderByOrderId(model.getOrderId()))
.withSelfRel();
model.add(selfLink);
Link ordersLink =
linkTo(methodOn(OrderController.class)
.getOrders())
.withRel("All orders");
model.add(ordersLink);
}
@Mapping(expression = "java(order.getOrderIdentifier().getOrderId())", target = "orderId")
@Mapping(expression = "java(order.getProductIdentifier().getProductId())", target = "productId")
@Mapping(expression = "java(order.getCustomerIdentifier().getCustomerId())", target = "customerId")
@Mapping(expression = "java(order.getEmployeeIdentifier().getEmployeeId())", target = "employeeId")
List<OrderResponseModel> entityListToResponseModelList(List<Order> orders);
}
还有一些我觉得可能与我的问题无关的其他课程,但我将它们包括在内是为了更好地衡量。
订单响应模型
package com.example.clothingstore.ordersubdomain.presentationlayer;
import com.example.clothingstore.common.enums.DeliveryStatus;
import com.example.clothingstore.common.identifiers.CustomerIdentifier;
import com.example.clothingstore.common.identifiers.EmployeeIdentifier;
import com.example.clothingstore.common.identifiers.OrderIdentifier;
import com.example.clothingstore.common.identifiers.ProductIdentifier;
import lombok.*;
import org.springframework.hateoas.RepresentationModel;
import java.math.BigDecimal;
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class OrderResponseModel extends RepresentationModel<OrderResponseModel> {
String orderId;
String productId;
String name;
BigDecimal price;
String customerId;
String streetAddress;
String postalCode;
String city;
String province;
String employeeId;
String lastName;
String deliveryStatus;
BigDecimal shippingPrice;
BigDecimal totalPrice;
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
}
架构:
CREATE TABLE IF NOT EXISTS orders (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
order_id VARCHAR(36) UNIQUE,
product_id VARCHAR(36),
name VARCHAR(50),
price DECIMAL(10, 2),
customer_id VARCHAR(36),
street_address VARCHAR(50),
postal_code VARCHAR(50),
city VARCHAR(50),
province VARCHAR(50),
employee_id VARCHAR(36),
last_name VARCHAR(50),
delivery_status VARCHAR(50),
shipping_price DECIMAL(10, 2),
total_price DECIMAL(10, 2),
FOREIGN KEY (product_id) REFERENCES products(product_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (employee_id) REFERENCES employees(employee_id),
INDEX idx_customer_id (customer_id),
INDEX idx_employee_id (employee_id),
INDEX idx_order_id (order_id)
);
getOrders 函数:
@Override
public List<OrderResponseModel> getOrders() {
List<Order> orders = orderRepository.findAll();
return orderResponseMapper.entityListToResponseModelList(orders);
}
我尝试自己添加 getter 和 setter,而不使用 lombok。我尝试将我的entityListToResponseModel 映射。我尝试了我的请求映射器但没有成功。
预先感谢您的帮助!如果这个问题不符合标准,我深表歉意,这是我第一次在这里发帖。
是的,您只需创建一个 forEach (在 orderServiceImpl getAllOrders() 函数中)就能够单独对所有条目使用EntityListToResponseModel,而不是使用entityListToResponseModelList 来正确且完整地映射每个订单。
最终只是摆脱了该死的功能并在我的服务实现中实现它。将代码留在这里给可能需要它的人!每当需要列出多个聚合/实体时,entityListToResponseModelList 似乎都会感到困惑
@Override
public List<OrderResponseModel> getOrders() {
List<OrderResponseModel> orderResponseModelList = new ArrayList<>();
List<Order> orders = orderRepository.findAll();
orders.forEach(order -> {
if(!productRepository.existsProductByProductIdentifier_ProductId(order.getProductIdentifier().getProductId()))
throw new InvalidInputException(("Invalid input for productId: ") + order.getProductIdentifier().getProductId());
if(!customerRepository.existsCustomerByCustomerIdentifier_CustomerId(order.getCustomerIdentifier().getCustomerId()))
throw new InvalidInputException(("Invalid input for customerId: ") + order.getCustomerIdentifier().getCustomerId());
if(!employeeRepository.existsEmployeeByEmployeeIdentifier_EmployeeId(order.getEmployeeIdentifier().getEmployeeId()))
throw new InvalidInputException(("Invalid input for employeeId: ") + order.getEmployeeIdentifier().getEmployeeId());
orderResponseModelList.add(orderResponseMapper.entityToResponseModel(order,
productRepository.findByProductIdentifier_ProductId(order.getProductIdentifier().getProductId()),
customerRepository.findByCustomerIdentifier_CustomerId(order.getCustomerIdentifier().getCustomerId()),
employeeRepository.findByEmployeeIdentifier_EmployeeId(order.getEmployeeIdentifier().getEmployeeId())));
});
return orderResponseModelList;
}