我的@RestController类中包含以下代码:
I have following code in my @RestController class:
@RequestMapping("api/") @RestController public class RecommendationsController { @PostMapping(path = "cart") public List<RecommendationDTO> getCartRecommendations(@NonNull @RequestBody List<CartItemDTO> cart){ System.out.println(cart); return null; } }这是我的CartItemDTO类中的代码:
This is the code in my CartItemDTO class:
public class CartItemDTO { private String productId; private Double quantity; public CartItemDTO(String productId, Double quantity) { this.productId = productId; this.quantity = quantity; } public String getProductId() { return productId; } public Double getQuantity(){ return quantity; }这是我与邮递员发送的请求:
And this is the request that I send with postman:
[ { "productId": "20000010", "quantity": 5.0; }, { "productId": "20000011", "quantity": 7.0; } ]这是可行的,但是当我更改代码时,如下所示,我收到错误的请求:错误的语法
This is working but when I change my code like following, I get Bad Request: bad syntax
public class CartItemDTO { private String productId; public CartItemDTO(String productId) { this.productId = productId; } public String getProductId() { return productId; } }有请求:
[ { "productId": "20000010" }, { "productId": "20000011" } ]有人知道可能是什么
推荐答案主要问题是Jackson无法构造DTO实例。 两种方法解决此问题: 1.指定默认构造函数: -指定参数化构造函数时,Java编译器将不会添加默认构造函数。
The main issue is Jackson unable to construct instance of a DTO. Two ways to solve this issue: 1. Specify the default constructor: - when you specify parameterized constructor, then the Java compiler will not add default constructor.
现在您的第一个请求:
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时,一切都按预期工作。
on adding default constructor to DTO, everything works fine as expected.
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添加为输出:
Since OP does not have RecommendationDTO object, adding just System.out.println as output:
[hello.dto.CartItemDTO@145e35d6, hello.dto.CartItemDTO@25df553f]
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更改为
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的
OR with only 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; } }更多推荐
将类从2更改为1时的Spring POST 400 Bad Request Postman
发布评论