我正在发送JSON对象,以将其持久保存在数据库中。我认为该类别已经存在并且无法修改(例如,使用下拉列表设置值)。
{
"productKeyId": "W7cpgwmb8LOkE7u5vRZLVoY8mXWmnR",
"name": "Pizzaé miam miam",
"price": 344.0,
"qty": 15,
"imgPath": "new/pathImage",
"category": {
"categoryKeyId": "VMz7EM6tNfoOAQtO1SHPYcH14jj0Cy",
"name": "Fish"
}
}
要构建此json,我创建了两个模型类:
@Getter @Setter
public class ProductCreateRequestModel {
private String productKeyId;
private String name;
private double price;
private int qty;
private String imgPath;
private CategoryRequestCreateProductModel category;
}
并且:
@Getter @Setter
public class CategoryRequestCreateProductModel {
private String name;
private String categoryKeyId;
}
在我的控制器中:
public ProductRest createProduct(@RequestBody ProductCreateRequestModel productRequestModel) throws Exception {
ProductRest returnValue = new ProductRest();
CategoryRequestCreateProductModel categoryRequestCreateProductModel = new CategoryRequestCreateProductModel();
//CategoryRest categoryRest = new CategoryRest();
ModelMapper modelMapper = new ModelMapper();
CategoryDto categoryDto = modelMapper.map(productRequestModel.getCategory(), CategoryDto.class);
if(productRequestModel.getName().isEmpty() || productRequestModel.getPrice() <= 0 || productRequestModel.getCategory() == null)
throw new ApplicationServiceException(ErrorMessages.MISSING_REQUIRED_FIELDS.getErrorMessage());
ProductDto productDto = new ProductDto();
productDto.setCategory(categoryDto);
productDto = modelMapper.map(productRequestModel, ProductDto.class);
ProductDto createdProduct = productService.createProduct(productDto);
returnValue = modelMapper.map(createdProduct, ProductRest.class);
return returnValue;
}
最后在我的服务层:
@Override
public ProductDto createProduct(ProductDto productDto) {
if ( productRepository.findByName(productDto.getName()) != null)
throw new ApplicationServiceException("Record already in Database");
CategoryDto categoryDto = productDto.getCategory();
productDto.setCategory(categoryDto);
ModelMapper modelMapper = new ModelMapper();
ProductEntity productEntity = modelMapper.map(productDto, ProductEntity.class);
String productKeyId = utils.generateProductKeyId(30);
productEntity.setProductKeyId(productKeyId);
ProductEntity storedProduct = productRepository.save(productEntity);
return modelMapper.map(storedProduct, ProductDto.class);
}
我的ProductDto层:
private Long id;
private String name;
private String productKeyId;
private double price;
private int availableQty;
private String imgPath;
private CategoryDto category = new CategoryDto();
我的CategoryDto:
private long id;
private String categoryKeyId;
private String name;
private CategoryDto parentCategory;
private List<CategoryDto> subCategories;
private String parentCategoryKeyId;
private Long parentCategoryId;
最后我的productEntity
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String productKeyId;
// many to one relationship with category
@ManyToOne
@JoinColumn(name = "category_id")
private CategoryEntity category;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private double price;
@Column(nullable = false)
private int availableQty;
private String imgPath;
还有我的CategoryEntity
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@Column(length = 30, nullable = false)
private String categoryKeyId;
@Column(nullable = false)
private String name;
@ManyToOne(optional = true, fetch = FetchType.LAZY)
@JoinColumn(name="parent_id", nullable=true)
private CategoryEntity parentCategory;
// allow to delete also subcategories
@OneToMany(mappedBy="parentCategory", cascade = CascadeType.ALL)
private List<CategoryEntity> subCategories;
//Here mappedBy indicates that the owner is in the other side
@OneToMany(fetch = FetchType.EAGER, mappedBy = "category", cascade = CascadeType.REMOVE)
private List<ProductEntity> products;
当我运行应用程序并发送请求时,我获得:
1) Converter org.modelmapper.internal.converter.NumberConverter@57364b9e failed to convert java.lang.String to long.
1 error] with root cause
java.lang.NumberFormatException: For input string: "VMz7EM6tNfoOAQtO1SHPYcH14jj0Cy"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:na]
at java.base/java.lang.Long.parseLong(Long.java:692) ~[na:na]
at java.base/java.lang.Long.valueOf(Long.java:1144) ~[na:na]
答案 0 :(得分:1)
问题是反序列化时,请尝试解决此问题。
public ProductRest createProduct(@RequestBody ProductCreateRequestModel productRequestModel) throws Exception {
ProductRest returnValue = new ProductRest();
CategoryRequestCreateProductModel categoryRequestCreateProductMode = modelMapper.map(productRequestModel.getCategory(), CategoryRequestCreateProductModel.class);
CategoryDTO categoryDTO = new CategoryDTO();
categoryDTO.setCategoryKeyId(categoryRequestCreateProductMode.getCategoryKeyId());
categoryDTO.setName(categoryRequestCreateProductMode.getName());
----
}