I am trying to find a way to set a category for a product over category_id but I don't know how could I use to id to set the category
My productDTO with just the basics:
public class ProductDTO {
private Integer id;
private String title;
private Category category;
private BigDecimal price;
private LocalDateTime createdAt;
private Integer category_id;
My product service
public class ProductService {
@Autowired
ProductRepository productRepository;
@Autowired
ProductMapper productMapper;
public void addProduct(ProductDTO productDTO){
productDTO.setCreatedAt(LocalDateTime.now());
productDTO.setCategory(productDTO.getCategory());
Product productEntity= productMapper.convertToEntity(productDTO);
productRepository.save(productEntity);
}}
I tried doing it with this service class but it obviously couldn't work...
The error :
"org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of pavicevic.koArtwebshop.entity.Category (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (1); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException"
Edit: Here is my POST method
@PostMapping(value = "api/products/add")
public ResponseEntity<Void> createProduct(@RequestBody ProductDTO productDTO) {
productService.addProduct(productDTO);
return new ResponseEntity<>(HttpStatus.CREATED);
}