I’m trying to develop an e-commerce website. Yesterday, I added Cart and CartItem to my project. I was able to create an address for the user before implementing the cart and cartItem functionality, but now I can’t.
Here are the relevant parts of my code related to the creating address issue,
The error which im dealing with is,
data: error: "Unsupported Media Type" message: "Content-Type 'application/json;charset=UTF-8' is not supported." path: "/address/createAddress/1" status: 415
@Entity
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Table(name = "Address")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "address_id", nullable = false, updatable = false)
private Long id;
... (basic plain other variables such as firstName, lastName etc.)
@ManyToOne
@JoinColumn(name = "user_id")
@JsonBackReference(value = "userAddresses")
private User user;
}
@RestController
@RequestMapping("/address")
@RequiredArgsConstructor
public class AddressController {
private final AddressService addressService;
@PostMapping(path = "/createAddress/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
public void createAddress(@PathVariable Long id, @RequestBody Address newAddress){
addressService.createAddress(id, newAddress);
}
...
AddressServiceImpl for creating an address
@Override
public void createAddress(Long userId, Address newAddress) {
User user = userRepository.findById(userId).orElseThrow(UserNotExistsException::new);
List<Address> userAddresses = addressRepository.findAllByUserId(user.getId());
if(userAddresses.isEmpty()){
newAddress.setDefault(true);
}
newAddress.setUser(user);
addressRepository.save(newAddress);
}
And the other problem which I’m dealing is,
Failed to evaluate Jackson deserialization for type [[simple type, class com.project.supplement.entity.Address]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'productFlavours': back reference type (
java.util.List<com.project.supplement.entity.Flavour>) not compatible with managed type (com.project.supplement.entity.Flavour)
And here are the Product and Flavour entities,
@Entity
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "product_id", updatable = false, nullable = false)
private Long id;
...
@OneToMany(mappedBy = "product",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonManagedReference(value = "productNutritionFacts")
private List<NutritionFacts> NutritionFacts;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
@JsonBackReference(value = "productCategory")
private Category category;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.LAZY)
@JoinTable(
name = "product_flavour",
joinColumns = @JoinColumn(name = "product_id"),
inverseJoinColumns = @JoinColumn(name = "flavour_id")
)
@JsonBackReference(value = "productFlavours")
private List<Flavour> flavours;
@ElementCollection
private List<String> ingredients;
...
@ManyToMany(mappedBy = "favorites")
private List<User> favoritedByUsers;
}
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Table(name = "flavour")
public class Flavour {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "flavour_id", updatable = false, nullable = false)
private Long id;
...
@ManyToMany(mappedBy = "flavours")
@JsonManagedReference(value = "productFlavours")
private List<Product> products;
}
For the address creation issue, I attempted to resolve it by adding the following consumes parameter, but it didn’t work
consumes = {“application/json;charset=UTF-8”, MediaType.APPLICATION_JSON_VALUE})
For the Jackson deserialization warning, I assigned the same value to all entities whose variables contain JsonBackReference and JsonManagedReference.