Spring Data keyvalue example 2017-05-15 21:31

Spring Data KeyValue framework use java.util.Map to store data in memory. I will show you how use it in this page.

pom file


Java code

public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

@RequestMapping(path = "/spring-data-keyvalue")
public class MainController {

    private PersonRepository personRepository;

    @GetMapping(path = "/add")
    public String addNewUser(@RequestParam String name, @RequestParam String chatId) {
        Person person = new Person();
        return "Saved";

    @GetMapping(path = "/all")
    public Iterable<Person> getAllPerson() {
        return personRepository.findAll();

public class Person {
    private Integer id;
    private String name;
    private Integer chatId;

    //getter and setter method

public interface PersonRepository extends CrudRepository<Person, Long> {

Run the main method to start Spring Boot.

Access "http://localhost:8080/spring-data-keyvalue/add?name=henryxi1&chatId=1" to save the data in memory.

Access "http://localhost:8080/spring-data-keyvalue/all" to get the data. The output is like following.