将类从2更改为1时的Spring POST 400 Bad Request Postman [英] Spring POST 400 Bad Request Postman when changing class from 2 to 1 parameter

查看:445
本文介绍了将类从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]




  1. 仅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]




  1. 解决方案二:需要指示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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆