top of page

Create SpringBoot RestfulAPI using swagger,H2Database and JPA

Updated: Mar 18, 2021


Requirements : Eclipse,Spring Tool Suite


Dependencies : Swagger, H2 Database, JPA,Web

Build Project with the help of STS so you

will get project structure like this. first

ensure that you must hdependencies

in pom.xml file.


After,that create a package and class according to given picture.
















Pom.xml


SpringSwaggerJpaRestH2Application class


EmployeeRestController class

Employee class


EmployeeServiceImpl class




EmployeeRepository class


EmployeeService class


application.properties


copy this line in this file so that you configure H2 database will configure automatically in your application .and if you have any port problem like your port 8080 is used by some other resource then add server.port=8089 or any other port no. so that this app run.


spring.h2.console.enabled=true

spring.h2.console.path=/h2

server.port=8089


Now open H2 Databse with this url http://localhost:8089/h2/ in my case becuase i am using port no 8089.


make sure JDBC URL :jdbc:h2:mem:testdb otherwise u will get an error .then click on coonect then u will get an empty table called Employee. you can see by writing this query on it.



Now Test the Restful endpoints with swagger.

Url : http://localhost:8089/swagger-ui.html


Now click on employee-rest-controller then you will see operations and we will test all.

By Clicking on POST you can insert data into table.


Inserting into table click on Try it out. and when data is sucessfully inserted then you can see response code is 200.

Now check whether data is inserted into table or not go to H2 database.and run the command and the data is inserted into Employee table.

Test GET to see all employee details .


click on GET employee by id =1 and you will get details coressponding to that id.


you can update record of any Employee by providing id.


lets check the data is updated or not into table. yes updated

Test delete by Id =1


lets check the id =1 is deleted from table or not .yes its deleted.


留言


bottom of page