Skip to main content
spring-boot logo

Spring Boot REST web service – Part 1 – Spring Data and MySQL

spring-boot logo

In this tutorial series, we are going to discuss developing a REST web service using spring-boot. There is a tutorial for Simple REST Service using spring-boot. We are going to continue that tutorial.

What we will need

  • JDK 1.8
  • Maven 3
  • spring-boot 1.5.1
  • MySQL database – (You can use your favorite one)

Create a database

Create a table that name is “user” using following scripts and insert test data.

Add Spring-boot and MySQL connector dependency

Database and Spring Configuration

Create a package structure and add the application.properties into the resources directory.

 

Note :- use same property keys. The spring boot automatically configures it.

Note :- If you use com.mysql.jdbc.Driver, got this error

Use latest driver name (com.mysql.cj.jdbc.Driver) to fix this issue.

Now we are going to add the domain, repository and controller classes. Here’s the tricky configuration.

@SpringBootApplication – This annotation automatically detects all the @Entity, @Repository, @Controller/ @RestController, @Service and @Component. But all components must be in same or child package level. In this example, the application.java in separate the package so that we have to explicitly define the domain, repository and controllers.

here the example with explicitly defines packages.

 

 

@ComponentScan – use for scanning the components. In this example, controllers and services can not be identified, if we don’t define the component scan path.

@EnableJpaRepositories – use for identified the repositories.

@EntityScan – use for identified entities.

Entity & Repository

Now, we are ready to create an entity class and repository.

Create an User.java in com.mydevgeek.domain.

Then create a UserRepository.java in com.mydevgeek.repo

Spring data supports following interfaces.

CrudRepository – provides basic crud functionalities.
JpaRepository – provides JPA related functionalities such as persist, flush, batch delete, etc.
PagingAndSortingRepository – provides additional functionalities such as paging and sorting.

You can use CrudRepository or JpaRepository for this example.

Controller

Create a new java class. It’s called UserController.java in com.mydevgeek.controller.

At this moment our web service supports only get user details that we already inserted.

How to run it

First of all, make sure database is up and run. Then we can run our service via command line or IntelliJ.

  • Command Line – go to the project directory. Run “mvn spring-boot:run
  • IntelliJ – Build the project. Then, right click on the Application.java class and select the “Run“.

If you clearly look at the console. We can see, there is an API for “/user/{id}”. This is a good way to identify whether rest API running or not.

How to test it

Send GET request through the POSTMAN.

Response

Project code :- GitHub

Next tutorial will be how to implement CRUD operations and some best practices.


Related Post

7725total visits,27visits today

RSS
Follow by Email
Facebook
Facebook
Google+
http://mydevgeek.com/spring-boot-rest-web-service-part-1-spring-data-mysql
Twitter