【问题标题】:How to pass the list of integers as json data to spring boot rest API?如何将整数列表作为json数据传递给spring boot rest API?
【发布时间】:2020-12-18 08:11:06
【问题描述】:

如何传递customerids和帐户详细信息数组并在控制器中接收?

下面是控制器的冷门。

控制器

@PostMapping("/createaccount")
public String createAccount(@RequestBody Customerids customerids,@RequestBody Account account)
    {
        return accountservices.createAccountService(customerids, account);
    }

只是想知道下面给出的json格式对吗?

JSON 已通过:

{
    "customerids" : {
        "customreids" : [15,16,17]
    },
    "account":{
    "type": "savings",
    "individual":"no",
    "balance": 3000.0


    }   
    
}

帐号:

package com.tracker.pojos;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name="Account")
public class Account {
    
    
    @Id
    private String account_number;
    
    @Column
    private String type;
    
    @Column
    private String individual;
    
    @Column
    private double balance;
    
    @OneToMany
    private List<Customer> customers;
    
}

**客户ID:**

package com.tracker.pojos;

import java.util.List;

import lombok.*;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Customerids {
    List<Integer> customerids;
}

抛出错误: 2020-12-18 13:43:02.491 WARN 10776 --- [nio-8080-exec-2] .wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法反序列化 @987654325 的实例@ out of START_OBJECT 令牌;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize of java.util.ArrayList&lt;java.lang.Integer&gt; out of START_OBJECT token 在 [来源:(PushbackInputStream); line: 2, column: 21](通过引用链:com.tracker.pojos.Customerids["customerids"])]

【问题讨论】:

  • 一个请求只有一个主体。您希望如何将相同的内容编组为 2 个班级?这显然行不通。
  • 我怎样才能让它工作?是否可以在一个请求中发送两个对象?
  • 创建一个包含所有信息的 DTO,然后将其映射到您的实体。

标签: java json spring controller rest


【解决方案1】:

@RequestBody 注释读取整个请求正文,在您的代码中,您使用它两次来读取请求正文的两个不同部分。你不能那样实现它。最好定义一个新的 DTO 类,其中包含您需要在请求正文中接收的所有内容,然后您可以从该 DTO 中读取您想要的所有内容,可以如下完成:

@Getter
@Setter
public class CreateAccountModel {

    private Integer[] customerIds;
    private Account account;
}

这将是您的终点:

@PostMapping("/createaccount")
public String createAccount(@RequestBody CreateAccountModel createAccountModel) {
    return accountservices.createAccountService(
            createAccountModel.getCustomerIds(),
            createAccountModel.getAccount());
}

然后您可以发送如下请求:

curl -i -H "Content-Type:application/json" -d '{"customerIds": [1,2,3], "account": {}}' http://localhost:8080/createaccount

【讨论】:

    【解决方案2】:

    您的 pojo 中的 customerIds 需要一个对象,并且该对象绑定到一个数组。然而,您的 Accounts 类绑定到一个 List,它是一个数组。

    改变这个:

    "customerids" : {
        "customreids" : [15,16,17]
    },
    

    仅此:

    "customerids" : [15,16,17],
    

    然后改变这个:

    @OneToMany
    private List<Customer> customers;
    

    到这里:

    @OneToMany
    private List<Customer> customerIds;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 2018-03-09
      • 2020-05-31
      相关资源
      最近更新 更多