Основная проблема заключается в том, что Джексон не может создать экземпляр DTO.
Два способа решения этой проблемы:
1. Укажите конструктор по умолчанию:
- когда вы задаете параметризованный конструктор, тогда Java компилятор не добавит конструктор по умолчанию .
Теперь ваш первый запрос:
curl --location --request POST 'localhost:8080/cart' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"productId": "20000010",
"quantity": 5.0
},
{
"productId": "20000011",
"quantity": 7.0
}
]'
Выдает ошибку:
"timestamp": "2020-04-27T12:08:28.497+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Type definition error: [simple type, class hello.dto.CartItemDTO]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `hello.dto.CartItemDTO` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 3, column: 9] (through reference chain: java.util.ArrayList[0])",
"path": "/cart"
при добавлении конструктора по умолчанию до DTO все работает нормально, как и ожидалось.
public class CartItemDTO {
private String productId;
private Double quantity;
public CartItemDTO() {
}
public CartItemDTO(String productId, Double quantity) {
this.productId = productId;
this.quantity = quantity;
}
public String getProductId() {
return productId;
}
public Double getQuantity() {
return quantity;
}
}
Поскольку у OP нет объекта RecommendationDTO
, в качестве вывода добавляется только System.out.println:
[hello.dto.CartItemDTO@145e35d6, hello.dto.CartItemDTO@25df553f]
Только ProductId в DTO
public class CartItemDTO {
private String productId;
public CartItemDTO() {
}
public CartItemDTO(String productId, Double quantity) {
this.productId = productId;
}
public String getProductId() {
return productId;
}
}
Запрос:
curl --location --request POST 'localhost:8080/cart' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"productId": "20000010"
},
{
"productId": "20000011"
}
]'
Вывод:
[hello.dto.CartItemDTO@42ad1f23, hello.dto.CartItemDTO@777d8eb3]
Решение второе: необходимо указать Джексону создать объект dto, используя конструктор с полями экземпляра, как показано ниже:
изменить DTO на
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class CartItemDTO {
private String productId;
private Double quantity;
@JsonCreator
public CartItemDTO(@JsonProperty(value = "productId", required = true) String productId,
@JsonProperty(value = "quantity", required = true) Double quantity) {
this.productId = productId;
this.quantity = quantity;
}
public String getProductId() {
return productId;
}
public Double getQuantity() {
return quantity;
}
}
ИЛИ только с productId
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class CartItemDTO {
private String productId;
@JsonCreator
public CartItemDTO(@JsonProperty(value = "productId", required = true) String productId) {
this.productId = productId;
}
public String getProductId() {
return productId;
}
}