Spring Boot rest API POST list of objects

In this post well see how to bind a list of objects in Spring MVC so that the objects in that List can be displayed in the view part.

Spring MVC Project structure using Maven

  • Please refer Spring Web MVC Example With Annotations for getting the project structure using Spring XML configuration.
  • Please refer Spring Web MVC Java Configuration Example for getting the project structure using Spring Java configuration.

Maven Dependencies

JSTL tags are also used in this Spring MVC example for binding list of objects so you need to add the following Maven dependency for JSTL apart from Spring dependencies.

jstl jstl 1.2

It adds the following jar-

jstl-1.2.jar
Table of contents
  1. Spring MVC binding List example Required XML Configuration
  2. Spring MVC binding List example Model classes
  3. Spring MVC binding List example Controller class
  4. Spring MVC binding List example Views

Spring MVC binding List example Required XML Configuration

Since JSTL tags are used in JSP so you need your view to resolve to JstlView, for that you need to add viewClass property in the bean definition for InternalResourceViewResolver in your DispatcherServlet configuration.

Spring MVC binding List example Model classes

List stores objects of the User class which is defined as given below.

public class User { private String firstName; private String lastName; private String email; public User[] {} public User[String firstName, String lastName, String email] { this.firstName = firstName; this.lastName = lastName; this.email = email; } public String getFirstName[] { return firstName; } public void setFirstName[String firstName] { this.firstName = firstName; } public String getLastName[] { return lastName; } public void setLastName[String lastName] { this.lastName = lastName; } public String getEmail[] { return email; } public void setEmail[String email] { this.email = email; } }

Following class acts a container for the List of User objects.

public class UserListContainer { private List users; public List getUsers[] { return users; } public void setUsers[List users] { this.users = users; } }

Spring MVC binding List example Controller class

@Controller public class UserController { @RequestMapping[value = "/getUsers", method = RequestMethod.GET] public String getUsers[Model model] throws Exception{ List users = getListOfUsers[]; UserListContainer userList = new UserListContainer[]; userList.setUsers[users]; model.addAttribute["Users", userList]; return "showUsers"; } // Dummy method for adding List of Users private List getListOfUsers[] { List users = new ArrayList[]; users.add[new User["Jack", "Reacher", ""]]; users.add[new User["Remington", "Steele", ""]]; users.add[new User["Jonathan", "Raven", ""]]; return users; } }

In the controller class there is a handler method getUsers[] where a list of users is created and set to the UserListContainer which in turn is added as an attribute to the Model. Logical view name returned from the method is showUsers which resolves to a JSP at the location WEB-INF\jsp\showUsers.jsp.

Spring MVC binding List example Views

If you just want to iterate the list and show the object fields then you can use the given JSP.

Spring MVC List of objects display First Name Last Name Email
${user.firstName} ${user.lastName} ${user.email}

If you want to iterate the list, show the object fields and want to bind the List of objects again to modelAttribute then you can use the following JSP.

Spring MVC List of objects display First Name Last Name Email

In this JSP Spring form tags are used for Spring MVC form fields and for looping the List JSTL tag is used. For these tag libraries following lines are added in the JSP.

To verify that the List of users is added to the Model and can be accessed in the handler method you can add the following method in the Controller class.

@RequestMapping[value = "/saveUsers", method = RequestMethod.POST] public void saveUsers[@ModelAttribute["Users"] UserListContainer userList] throws Exception{ List users = userList.getUsers[]; for[User user : users] { System.out.println["First Name- " + user.getFirstName[]]; } }

Once the application is deployed it can be accessed using the URL - //localhost:8080/spring-mvc/getUsers

Just showing the object fields

Showing the object fields and binding to Model

Recommendations for learning

  1. Spring Framework Master Class Course
  2. Spring & Hibernate for Beginners [Includes Spring Boot]
  3. Java In-Depth: Become a Complete Java Engineer!
  4. Complete Python Bootcamp Course
  5. React - The Complete Guide Course

That's all for this topic Spring MVC - Binding List of Objects Example. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page

Related Topics

  1. Spring MVC File Download Example
  2. Spring MVC Exception Handling Example Using @ExceptionHandler And @ControllerAdvice
  3. Difference Between @Controller And @RestController Annotations in Spring
  4. Spring MVC Dot [.] Truncation Problem With @PathVariable Annotation
  5. Insert\Update Using JDBCTemplate in Spring Framework

You may also like-

  1. Spring Batch Processing With List of Objects in batchUpdate[] Method
  2. registerShutdownHook[] Method in Spring Framework
  3. ApplicationContextAware And BeanNameAware Interfaces in Spring Framework
  4. Autowiring Using Annotations in Spring
  5. Java Stream API Interview Questions
  6. Difference Between ArrayList And CopyOnWriteArrayList in Java
  7. Multiple Catch Blocks in Java Exception Handling
  8. Uber Mode in Hadoop

Video liên quan

Chủ Đề