Spring Boot Restful Service with JPA Configuration and H2 in-memory database

Hello everyone, Today we will learn how to create REST API using Spring Boot, JPA and H2 In-memory database 

Following technologies stack being used:
  • Spring Boot 2.1.1.RELEASE
  • Spring 5.1.3.RELEASE 
  • Maven 3
  • JDK 1.8
  • Eclipse Oxygen
  • Free Maker
  • Hibernate 5.3.7
  • HikariCP 3.2.0
  • H2 in-memory database 1.4.197
1)Project Structure


2)Maven/Dependency Management [pom.xml] 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<!-- lookup parent from repository -->
</parent>
<groupId>com.knowledgefactory</groupId>
<artifactId>springboot-CRUD-HSQLDB</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-CRUD-HSQLDB</name>
<description>Demo project for springboot-CRUD-HSQLDB</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring mvc, rest -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

3)Controller

package com.knowledgefactory.knowledgefactorydemo;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.knowledgefactory.Entity.User;
import com.knowledgefactory.Repository.UserRepository;
import com.knowledgefactory.VO.UserVo;

@RestController
public class HomeController {
@Autowired
UserRepository repository;

@GetMapping({ "/", "/getcall" })
public ResponseEntity<List<UserVo>> listAllUsers() {
List<User> userList = new ArrayList<>();
userList = (List<User>) repository.findAll();
List<UserVo> vo = new ArrayList<>();
for (User users : userList) {
UserVo obj = new UserVo();
obj.setId(users.getId());
obj.setName(users.getName());
vo.add(obj);
}
return new ResponseEntity<List<UserVo>>(vo, HttpStatus.OK);
}
}

4)Entity Class

package com.knowledgefactory.Entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;

public User() {
}

public User(String name) {
this.name = name;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public User(Long id, String name) {
super();
this.id = id;
this.name = name;
}
}

5)VO Class

package com.knowledgefactory.VO;

public class UserVo {
private String name;
private Long id;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}

6)Repository

package com.knowledgefactory.Repository;

import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.knowledgefactory.Entity.User;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {
List<User> findByName(String name);
}

7)Spring Boot

package com.knowledgefactory.knowledgefactorydemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.knowledgefactory.Entity.User;
import com.knowledgefactory.Repository.UserRepository;

@EnableJpaRepositories(basePackages = "com.knowledgefactory.Repository")
@SpringBootApplication
@ComponentScan({ "com" })
@EntityScan("com.knowledgefactory.Entity")
public class KnowledgefactorydemoApplication implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(KnowledgefactorydemoApplication.class);
@Autowired
private UserRepository repository;

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

@Override
public void run(String... args) throws Exception {
log.info("StartApplication...");
repository.save(new User("Knf"));
repository.save(new User("sibin"));
}
}

8)application.properties

logging.level.org.springframework=INFO
logging.level.com.knowledgefatory*=INFO
logging.level.com.zaxxer=DEBUG
logging.level.root=ERROR
server.port=8081

9)Run

$ mvn spring-boot:run

10)Test


by sibin
web solution consultant for the startups
+-91-8075866854

Comments