ModelMapper 无法将 java.lang.String 转换为 java.lang.Long
ModelMapper failed to convert java.lang.String to java.lang.Long
我有用于构建发送的 JSON 的休息模型。
一个休息模型
@Getter @Setter
@ToString
public class OrderRequestModel {
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<ProductRequestModel> orderProducts;
private UserDetailsRequestModel seller;
private Date createdAt;
}
ProductRequestModel 相似
@Getter @Setter
@ToString
public class ProductRequestModel {
private String productKeyId;
private String name;
private double price;
private int qty;
private String imgPath;
private CategoryRequestModel category;
}
我将模型传递给与数据库相关的 DTO 层(它们包含一个长 ID):
@Getter @Setter
@ToString
public class OrderDto implements Serializable {
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private Long id;
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<ProductDto> orderProducts;
private UserDto seller;
private Date createdAt;
}
还有我的 ProductDto:
@Getter @Setter
@ToString
public class ProductDto implements Serializable {
// ommit this member and do not generate getter / setter
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private Long id;
private String productKeyId;
private String name;
private double price;
private int qty;
private String imgPath;
private CategoryDto category = new CategoryDto();
}
当我尝试将 OrderDto 与关联模型映射时,我会隐式地进行:
OrderDto orderDto = modelMapper.map(orderRequestModel, OrderDto.class);
理论上,模型中的 orderKeyId 应与其在 Dto 中的等效项匹配。不幸的是 returns 一个错误:
Converter org.modelmapper.internal.converter.NumberConverter@3e36f4cc failed to convert java.lang.String to java.lang.Long.
Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:
我确实需要 DTO 中的 ID,因为如果我想进行更新,我会使用 "id"
此问题是由映射策略引起的。我们可以将 Mapping Strategies 设置为 STRICT,这样当从源对象 属性 映射到目标对象的 属性 时,它只映射那些 属性 与 属性 名称完全匹配的 属性以及它的数据类型。下面是一个例子。
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STRICT);
}
仅供参考:
http://modelmapper.org/user-manual/configuration/#matching-strategies
我有用于构建发送的 JSON 的休息模型。
一个休息模型
@Getter @Setter
@ToString
public class OrderRequestModel {
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<ProductRequestModel> orderProducts;
private UserDetailsRequestModel seller;
private Date createdAt;
}
ProductRequestModel 相似
@Getter @Setter
@ToString
public class ProductRequestModel {
private String productKeyId;
private String name;
private double price;
private int qty;
private String imgPath;
private CategoryRequestModel category;
}
我将模型传递给与数据库相关的 DTO 层(它们包含一个长 ID):
@Getter @Setter
@ToString
public class OrderDto implements Serializable {
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private Long id;
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<ProductDto> orderProducts;
private UserDto seller;
private Date createdAt;
}
还有我的 ProductDto:
@Getter @Setter
@ToString
public class ProductDto implements Serializable {
// ommit this member and do not generate getter / setter
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private Long id;
private String productKeyId;
private String name;
private double price;
private int qty;
private String imgPath;
private CategoryDto category = new CategoryDto();
}
当我尝试将 OrderDto 与关联模型映射时,我会隐式地进行:
OrderDto orderDto = modelMapper.map(orderRequestModel, OrderDto.class);
理论上,模型中的 orderKeyId 应与其在 Dto 中的等效项匹配。不幸的是 returns 一个错误:
Converter org.modelmapper.internal.converter.NumberConverter@3e36f4cc failed to convert java.lang.String to java.lang.Long.
Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:
我确实需要 DTO 中的 ID,因为如果我想进行更新,我会使用 "id"
此问题是由映射策略引起的。我们可以将 Mapping Strategies 设置为 STRICT,这样当从源对象 属性 映射到目标对象的 属性 时,它只映射那些 属性 与 属性 名称完全匹配的 属性以及它的数据类型。下面是一个例子。
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STRICT);
}
仅供参考: http://modelmapper.org/user-manual/configuration/#matching-strategies