by BehindJava

How to store user details temporarily in a Map to Handle HTTP GET request in Spring Boot

Home » springboot » How to store user details temporarily in a Map to Handle HTTP GET request in Spring Boot

In this tutorial we are going to learn about storing the user details in a Map based on Key and value pairs.

Firstly, in the controller class we are going to create a Map to store user details and this enables the Map to keep information the memory until we restart the Spring Boot application. The map contains key as a String and object is of type UserModel as shown in the below code snippet.

Map: An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.

The Map interface provides three collection views, which allow a map’s contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map’s collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

Sample Code Snippet:

import java.util.HashMap;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.behindjava.tutorial.model.UserModel;

public class UserController {

	Map<String, UserModel> user;

	@PostMapping(produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }, consumes = {
	public ResponseEntity<UserModel> createUser(@Valid @RequestBody UserModel us) {

		UserModel um = new UserModel();

		if (user == null)
			user = new HashMap<>();
		user.put(us.getUserId(), um);

		return new ResponseEntity<UserModel>(um, HttpStatus.CREATED);

	@GetMapping(path = "/{userId}", produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE })
	public ResponseEntity<UserModel> getUser(@PathVariable String userId) {

		if (user.containsKey(userId)) {
			return new ResponseEntity<UserModel>(user.get(userId), HttpStatus.CREATED);
		} else {
			return new ResponseEntity<UserModel>(HttpStatus.NO_CONTENT);


Here are the responses of HTTP POST and GET request where users details are sent in the body of the request and these details are stored in the map.


In the below images we are fetching the details of user with the userId.


Previous                                                                                                               Next