how to send a list of long to work with a rest controller
You actually send a json object here not a List
Send this in your post request instead.
[1,2,3]
Your previous request could work if you had something like this
public class X {
@JsonFormat (with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
private List<Long> project_id;
}
@PostMapping("person/{id}/projects/")
public Person addProject(@PathVariable Long id, @RequestBody X x){..}
How to pass the list of integers as json data to spring boot rest API?
@RequestBody
annotation read the whole request body, in your code you used it twice to read tow different parts of your request body. 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:
@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
Related Topics
How to Use Variables in One Method into Another Method
Parameter 0 of Constructor in Required a Bean of Type 'Java.Lang.String' That Could Not Be Found
@Responsebody , Responseentity Spring Return Object as Json
Null Pointer on an Autowired Bean Which Is Not Mocked by Mockito
Field Required a Bean Which Could Not Be Found in Springboot
How to Increase the Java Heap Size Permanently
Java:Using Apache Poi How to Convert Ms Word File to Pdf
Error About Sun/Misc/Base64Encoder on Eclipse
Could Not Open JPA Entitymanager for Transaction (Using Localcontainerentitymanagerfactorybean)
No Content to Map Due to End-Of-Input Jackson Parser
Jparepository Findall() Returns Empty Result
Rounding to the Nearest Hundered-Thousandths
How to Convert Linkedhashmap to Custom Java Object
Java: How to Print Array Without Square Brackets and Commas
Checking If a String Contains Only Digits (Isdigitmethod Required)
How to Get Numbers from Given Gcd and Lcm
Crudrepository How to Find by Last Date Between
How to Import Two Classes With the Same Name in Different Packages