java - 项目不能添加到列表中;
问题描述
我的代码有问题,我想返回这样的视图模型:
[
{
"attributeTypeId": "60af71c55977a92f8a586d43",
"attributeTypeTitle": "color",
"attributes": [
{
"id": "60af75235977a92f8a586d4d",
"title": "green",
"id": "60af75295977a92f8a586d4e",
"title": "blue",
}
]
},
{
"attributeTypeId": "60af71bd5977a92f8a586d42",
"attributeTypeTitle": "Size",
"attributes": [
{
"id": "60af72255977a92f8a586d46",
"title": "36"
}
]
它应该检查是否存在相同的 id,然后将其值添加到一个集合中,如果不是,则创建一个新的。所以当我发送我的请求时,我在服务和 viewModel 上遇到了这个错误。这是我的服务班
public Flux<List<AttributesViewModel>> findAttributeByCategoryId(String categoryId, Pageable pageable) {
Map<String, String> AttribTypeTitleMap = attributeTypeService.findAll()
.toStream()
.collect(Collectors.toMap(AttributeType::getId, AttributeType::getType));
Map<String, String> AttribValueTypeIdMap = attributeValueService.findAll()
.toStream()
.collect(Collectors.toMap(AttributeValue::getId , AttributeValue::getAttributeTypeId));
Map<String, String> AttribValueTitleMap = attributeValueService.findAll()
.toStream()
.collect(Collectors.toMap(AttributeValue::getId , AttributeValue::getValue));
var attributeList = new AttributesListViewModel();
return findProductsByCategoryId(categoryId)
.filter(product -> !product.getAttributeValueIdList().isEmpty())
.flatMap( product -> {
for (String attValId : product.getAttributeValueIdList().split(",")) {
var attribute = new AttributeViewModel(attValId, AttribValueTitleMap.get(attValId));
var attributeType = AttribTypeTitleMap.get(AttribValueTypeIdMap.get(attValId));
attributeList.addAttributeList(AttribValueTypeIdMap.get(attValId), attributeType, attribute);
}
return Flux.just(attributeList.getResults());
});
}
这些是我的视图模型:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AttributeViewModel {
String id;
String title;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AttributesViewModel {
String attributeTypeId;
String attributeTypeTitle;
Set<AttributeViewModel> attributes;
}
@Data
@AllArgsConstructor
@Slf4j
public class AttributesListViewModel {
List<AttributesViewModel> results;
public AttributesListViewModel() {
results = new ArrayList<>();
}
public void addAttributeList(String typeId, String typeValue, AttributeViewModel
attributes) {
for (AttributesViewModel item: results) {
if (item.getAttributeTypeId().equals(typeId)) {
item.getAttributes().add(attributes);
return;
}
}
results.add(new AttributesViewModel(typeId, typeValue, Set.of(attributes)));
}
}
当我发送请求时,我的服务有问题:
attributeList.addAttributeList(AttribValueTypeIdMap.get(attValId), attributeType,
attribute);
在我的第三个视图模型中:
item.getAttributes().add(attributes);
解决方案
推荐阅读
- docker - XDEBUG 无法正常工作,但已安装并且日志显示“连接到客户端 :-)”
- html - 如何使用 Tabulator.js 计算时间
- ruby-on-rails - 如何修复 Rails Geocoder 中的错误“未定义方法”?
- tensorflow - TensorFlow 对象检测 API:如何隔离主干网络?
- javascript - 循环无限嵌套对象并构造字符串
- javascript - sinon依赖函数伪造
- sap - 通过将 SAP 表拆分为小块来复制它
- json - 在 vb.net 中获取令牌值
- c# - JSON web-api 上暴露的对象 - 如何阻止属性名称更改大小写?
- html - 为什么 HTML 超链接在我的子文件夹中不起作用?