简体   繁体   English

如何将整数列表作为 json 数据传递给 spring 引导 rest ZDB974238714CA8DE634A7ACE14

[英]How to pass the list of integers as json data to spring boot rest API?

How to pass the array of customerids and account details and receive it in the controller?如何传递 customerids 和帐户详细信息数组并在 controller 中接收?

Below is the colde of the controller.下面是controller的colde。

Controller Controller

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

Just wanted to know is the below given json format is right?只是想知道下面给出的 json 格式是否正确?

JSON passed: JSON 通过:

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


    }   
    
}

Account:帐户:

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;
    
}

**Customerids: ** **客户编号:**

package com.tracker.pojos;

import java.util.List;

import lombok.*;

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

Error thrown: 2020-12-18 13:43:02.491 WARN 10776 --- [nio-8080-exec-2].wsmsDefaultHandlerExceptionResolver: Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.ArrayList<java.lang.Integer> out of START_OBJECT token; Error thrown: 2020-12-18 13:43:02.491 WARN 10776 --- [nio-8080-exec-2].wsmsDefaultHandlerExceptionResolver: Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.ArrayList<java.lang.Integer>出 START_OBJECT 令牌; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList<java.lang.Integer> out of START_OBJECT token at [Source: (PushbackInputStream); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList<java.lang.Integer> out of START_OBJECT token at [Source: (PushbackInputStream); line: 2, column: 21] (through reference chain: com.tracker.pojos.Customerids["customerids"])]行:2,列:21](通过参考链:com.tracker.pojos.Customerids["customerids"])]

@RequestBody annotation read the whole request body, in your code you used it twice to read tow different parts of your request body. @RequestBody注释读取整个请求正文,在您的代码中,您使用它两次来读取请求正文的两个不同部分。 You can not achieve it like that.你不能那样实现它。 It is better to define new DTO class that contains all the things that you need to receive in request body, then you can read everything you want from that DTO, it can be done like below:最好定义新的 DTO class ,其中包含您需要在请求正文中接收的所有内容,然后您可以从该 DTO 中读取您想要的所有内容,可以如下完成:

@Getter
@Setter
public class CreateAccountModel {

    private Integer[] customerIds;
    private Account account;
}

And this will be your endpoint:这将是您的端点:

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

Then you can sen request like below:然后你可以发送如下请求:

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

Your customerIds in your pojo expects an object and that object is bound to an array.您的 pojo 中的 customerIds 需要一个 object 并且 object 绑定到一个数组。 Yet your Accounts class is bound to a List which is an array.然而,您的 Accounts class 绑定到一个数组列表。

Change this:改变这个:

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

to just this:仅此:

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

and change this:并改变这个:

@OneToMany
private List<Customer> customers;

to this:对此:

@OneToMany
private List<Customer> customerIds;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How to pass a list of integers as json data to a Spring Boot rest API without a custom intermediate object? - How to pass a list of integers as json data to a Spring Boot rest API without a custom intermediate object? 如何在spring-boot数据休息时在POST json中传递@EmbeddedId - How to pass @EmbeddedId in POST json in spring-boot data rest 在Spring Boot Rest API中获取JSON数据 - fetch json data in spring boot rest api 如何将数据从 Angular 10 表单传递到包含字符串和文件/图像的 Spring Boot Rest API? - How to Pass Data from Angular 10 form to Spring Boot Rest API that includes both Strings and Files/Images? 如何在 Spring Boot 中持续接收和解析来自 REST API 的 JSON - How to continuously receive and parse the JSON from REST API in spring boot 如何在 Spring 引导 REST ZDB974238714CA8DE634A7CE1D083A14F 中格式化返回的 json? - How to format returned json in Spring Boot REST API? 如何在 Spring Boot Rest API 中以 XML 形式返回对象列表 - How to return a list of objects as XML in Spring boot rest API Spring Boot Rest API JSON 返回 null - Spring Boot Rest API JSON return null 如何使用 REST API - Spring 引导插入带有伪造密钥的数据 - How to insert data with forgein key using REST API - Spring boot 如何将参数传递给 Spring 引导 REST Controller? - How to pass an argument to a Spring Boot REST Controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM