首页 > 解决方案 > 项目不能添加到列表中;

问题描述

我的代码有问题,我想返回这样的视图模型:

   [
    {
        "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);

标签: javaspring-webflux

解决方案


推荐阅读