Create Project: springboot_httprequest_requestbody_json_array (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
Create Package: entities (inside main package)
– Create Class: Person.java (inside package controllers)
Person.java
package com.ivoronline.springboot_httprequest_requestbody_json_array.entities;
public class Person {
public String name;
public Integer age;
}
MyController.java
package com.ivoronline.springboot_httprequest_requestbody_json_array.controllers;
import com.ivoronline.springboot_httprequest_requestbody_json_array.entities.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/AddPersons")
public String addPersons(@RequestBody List<Person> persons) {
//ITERATE THROUGH LIST
String result = "";
for (Person person : persons) {
String name = person.name;
Integer age = person.age;
result += name + " is " + age + " years old \n";
}
//RETURN SOMETHING
return result;
}
}