mapstruct类型转换报错

时间:2025-01-26 22:42:02
// 前端传递的类 public class AddOrderParameter { private List<ProductDTO> products; // Money为自定义类型 @Mapper(imports = {Money.class}) public static class ProductDTO { private String name; private BigDecimal price; private Integer count; @Mapper public interface ProductDTOMapper { ProductDTOMapper INSTANCE = Mappers.getMapper(ProductDTOMapper.class); // 自定义转换,这里()是自定义方法,返回的是Optional类型 @Mapping(target = "price", expression = "java((()).get())") Order.Product to(ProductDTO productDTO); List<Order.Product> to(List<ProductDTO> productDTOs); } } // 注意不要漏,不然一直报错 @Mapper(uses = {ProductDTO.ProductDTOMapper.class}) public interface Convert { Convert INSTANCE = Mappers.getMapper(Convert.class); Order to(AddOrderParameter parameter); } } // 转换的类 public class Order { private List<Product> products; public static class Product { private String name; private Money price; private Integer count; } }