将类从2更改为1时的Spring POST 400 Bad Request Postman

编程入门 行业动态 更新时间:2024-10-23 22:25:03
本文介绍了将类从2更改为1时的Spring POST 400 Bad Request Postman的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的@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]

  • 仅DTO中的ProductId
  • 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]

  • 解决方案二:需要指示Jackson使用以下命令创建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的

    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

    本文发布于:2023-10-29 07:59:38,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1539152.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:POST   Spring   Postman   Request   Bad

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!