代码之家  ›  专栏  ›  技术社区  ›  davidvera

映射基本POJO的ModelMapper问题

  •  0
  • davidvera  · 技术社区  · 5 年前

    我有两个用于构建json对象的基本POJO:

    public class ProductCreateRequestModel {
        private String name;
        private double price;
        private int qty;
        private String imgPath;
    
        private CategoryRequestCreateProductModel category;
    }
    
    public class CategoryRequestCreateProductModel {
        private String name;
        private String categoryKeyId;
    }
    

    基本上,它允许我使用一个简单的json,如下所示:

    {
        "name": "Pizza,
        "price": 344.0,
        "qty": 15,
        "imgPath": "new/pathImage",
        "category": {
            "categoryKeyId": "23ume70Fu6yqyGUWfQkW110P4ko3gZ",
            "name": "Starter"
        }
    }
    

    public class ProductRest {
    
        private String productKeyId;
        private String name;
        private double price;
        private int qty;
        private String imgPath;
    
        private CategoryRest category;
    }
    

    在我的控制器中,我只需要调用一个使用PostMapping的方法

    @PostMapping(
            consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE },
            produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE }
    )
    public ProductRest createProduct(@RequestBody ProductCreateRequestModel productCreateRequestModel) throws Exception {
        ProductRest returnValue = new ProductRest();
        if(productCreateRequestModel.getName().isEmpty() || productCreateRequestModel.getPrice() <= 0)
            throw new ApplicationServiceException(ErrorMessages.MISSING_REQUIRED_FIELDS.getErrorMessage());
    
        ModelMapper modelMapper = new ModelMapper();
        ProductDto productDto = modelMapper.map(productCreateRequestModel, ProductDto.class);
    
        ProductDto createdProduct = productService.createProduct(productDto);
        returnValue = modelMapper.map(createdProduct, ProductRest.class);
    
        return returnValue;
    }
    

    @Override
    public ProductDto createProduct(ProductDto productDto) {
        return productDto;
    }
    

    我的DTO层包含以下字段:

    @Getter @Setter
    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 availableQty;
        private String imgPath;
    
        private CategoryDto category = new CategoryDto();
    }
    
    
    @Getter @Setter
    public class CategoryDto implements Serializable {
        @Getter(AccessLevel.NONE)
        @Setter(AccessLevel.NONE)
        private static final long serialVersionUID = 1L;
    
        private long id;
        private String categoryKeyId;
        private String name;
    
        private CategoryDto parentCategory;
        private List<CategoryDto> subCategories;
    
        private String parentCategoryKeyId;
    
        private Long parentCategoryId;
    }
    

    java.lang.NumberFormatException:对于输入字符串:“23ume70Fu6yqyGUWfQkW110P4ko3gZ”

    0 回复  |  直到 5 年前