gpt4 book ai didi

ModelMapper 无法将 java.lang.String 转换为 java.lang.Long

转载 作者:行者123 更新时间:2023-12-05 01:14:18 26 4
gpt4 key购买 nike

我有用于构建我发送的 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 中的等效项匹配。不幸的是它返回一个错误:

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”

最佳答案

此问题是由映射策略引起的。我们可以将映射策略设置为 STRICT,这样当从源对象属性映射到目标对象的属性时,它只映射那些在属性名称和数据类型上完全匹配的属性。下面是一个例子。

public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STRICT);
}

仅供引用: http://modelmapper.org/user-manual/configuration/#matching-strategies

关于ModelMapper 无法将 java.lang.String 转换为 java.lang.Long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58838964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com