【问题标题】:How can I write two objects one-to-many with dto class?如何使用 dto 类一对多地编写两个对象?
【发布时间】:2020-09-14 16:27:09
【问题描述】:

我想使用 dto 类保存两个通过关系连接的对象。当我对实体对象进行操作时,一切都很顺利,当我尝试这样做时,我得到一个错误。

字段“categoryDto”上的对象“产品”中的字段错误:拒绝值 [1];代码 [typeMismatch.product.categoryDto,typeMismatch.categoryDto,typeMismatch.com.lewandowski.multimy.dto.CategoryDto,typeMismatch]; 论据 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [product.categoryDto,categoryDto];论据 [];默认消息 [类别Dto]];默认消息 [无法转换的属性值 将“java.lang.String”键入所需类型 'com.lewandowski.multimy.dto.CategoryDto' 用于属性'categoryDto'; 嵌套异常是 java.lang.IllegalStateException: 无法转换 'java.lang.String' 类型的值到所需类型 属性“categoryDto”的“com.lewandowski.multimy.dto.CategoryDto”: 未找到匹配的编辑器或转换策略]]

我将补充一点,实体到 DTO 类的映射是正确的。下面我介绍表单所在的 Thymeleaf 代码。问题恰恰出在 select 字段和 CategoryDto 字段上。

<form action="#" th:action="@{/save-product}" th:object="${product}" method="post">
            <div class="form-group row">
                <label class="col-sm-3 col-form-label">Nazwa</label>
                <div class="col-sm-9">
                    <input type="text" th:field="*{name}" class="form-control" placeholder="Nazwa">
                </div>
            </div>

            **<div class="form-group row">
                <label class="col-sm-3 col-form-label">Kategoria</label>
                <div class="col-sm-9">
                    <select id="category" class="form-control" th:field="*{categoryDto}">
                        <option value="0">Wybierz Kategorie</option>
                        <option th:each="category: ${categoryList}"
                                th:value="${category.id}"
                                th:text="${category.name}">
                        </option>
                    </select>
                </div>
            </div>**
            <div class="form-group row">
                <label class="col-sm-3 col-form-label">Nazwa</label>
                <div class="col-sm-9">
                    <input type="text" th:field="*{technicalAttributesDto[0].name}" class="form-control" placeholder="Nazwa">
                </div>
            </div>
            
                <button style="width: 200px; margin-bottom: 20px" type="submit" class="btn btn-primary">Zapisz</button>
        </form>

控制器如下所示:

    @RequestMapping("/new-product")
    public String showNewProductPage(Model model) {
        model.addAttribute("product", new ProductDto());
        model.addAttribute("categoryList", categoryService.allCategoryDtoWithoutProduct());
        return "new_product";
    }
    @RequestMapping(value = "/save-product", method = RequestMethod.POST)
    public String saveProduct(@ModelAttribute("product") ProductDto product) {
        productService.save(product);
        return "redirect:/";
    }

【问题讨论】:

  • 请分享产品服务代码。那么,我们可以看到它实际上在做什么? stackTrace 的错误更容易接受。使用 Apache ExceptionUtils.getStackTrace(e)

标签: java spring-boot thymeleaf


【解决方案1】:

ProductService 包括将实体转换为 dto 和保存方法的方法

public void save(ProductDto productDto){
        List<TechnicalAttributesDto> technicalAttributesDtoList = new ArrayList<>();
        for (TechnicalAttributesDto technicalAttributesDto : productDto.getTechnicalAttributesDto()) {
            technicalAttributesDto.setProductDto(productDto);
            technicalAttributesDtoList.add(technicalAttributesDto);
        }

        technicalAttributesRepository.saveAll(technicalAttributesService.findAllTechnicalAttributes(technicalAttributesDtoList));
        productRepository.save(getProduct(productDto));
    }
    

    public List<Product> findAllProduct(List<ProductDto> productDtoList) {
        List<Product> productList = new ArrayList<>();
        for (ProductDto productDto : productDtoList) {
            Product product = getProduct(productDto);
            productList.add(product);
        }
        return productList;
    }
    public List<ProductDto> findAllProductDto(List<Product> productList) {
        List<ProductDto> productDtoList = new ArrayList<>();
        for (Product product : productList) {
            ProductDto productDto = getProductDto(product);
            productDtoList.add(productDto);
        }
        return productDtoList;
    }
    public Product getProduct(ProductDto productDto){
        Product product = Product.builder()
                .id(productDto.getId())
                .name(productDto.getName())
                .category(categoryService.getCategory(productDto.getCategoryDto()))
                .technicalAttributes(technicalAttributesService.findAllTechnicalAttributes(productDto.getTechnicalAttributesDto()))
                .build();
        return product;
    }
    public ProductDto getProductDto(Product product){
        ProductDto productDto = ProductDto.builder()
                .id(product.getId())
                .name(product.getName())
                .categoryDto(categoryService.getCategoryDto(product.getCategory()))
                .technicalAttributesDto(technicalAttributesService.findAllTechnicalAttributesDto(product.getTechnicalAttributes()))
                .build();
        return productDto;
    }

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多