Over a million developers have joined DZone.

How to Develop RESTful Web Services Using MySQL and Spring Boot

DZone's Guide to

How to Develop RESTful Web Services Using MySQL and Spring Boot

This article provides a 4-step tutorial on how to develop RESTful web services using MySQL and Spring Boot.

· Integration Zone ·
Free Resource

The State of API Integration 2018: Get Cloud Elements’ report for the most comprehensive breakdown of the API integration industry’s past, present, and future.

Spring Boot is the fastest way to develop a spring application. Let's see how to develop such an application that uses MySQL Database to store the path of text files and image files and then using Spring MVC as an architectural pattern and Spring RESTful web services to fetch images and text. All this will be done using Spring Boot.

Step 1: Changes for Spring Boot

[1] On IDE like STS, Select NEW-> Project -> Spring Starter Project

Image title[2] We will get a screen as below (make names of project packages as desired, click next): 

Image title

[3] Select Spring Project Dependencies as MySQL, JPA, and Web. Click finish: 

3 Project Dependencies

[4] We will get project Structure as below: 

4 Project Structure

Here, we have obtained a Spring Boot application which uses features of

1. JPA: so that we can map java entities/Classes with MySQL Tables. 

2. MySQL: as we are using MySQL Database here.

3. Web: This dependency helps us to get restful endpoints to Fetch Images and Text on Web Browser 

Step 2: Changes According to Spring MVC Architecture 

 Next step is to make use of Spring MVC Architecture and design our Controller, Dao, and Service Layers. 

[1] Controller: ApplicationController.java

@RequestMapping(value = "/application")
public class ApplicationController {

private ApplicationService applicationService;

@RequestMapping(value = "/get-image", method = RequestMethod.GET)
public ResponseEntity<byte[]> getImagePath() throws IOException {
ResponseEntity<byte[]> responseEntity = applicationService.getImageURL();
return responseEntity;

@RequestMapping(value = "/get-text", method = RequestMethod.GET)
public ResponseEntity<byte[]> getTextPath() throws IOException {
ResponseEntity<byte[]> responseEntity = applicationService.getTextData();
return responseEntity;

[2] ApplicationDao.java: 

public interface ApplicationDao extends CrudRepository<RequestData,Integer> {
@Query(value = "SELECT * FROM resource_table WHERE File_id=?1", nativeQuery = true)
  RequestData findResource(Integer i);

[3] ApplicationService.java: (Note: I am using 1 as an argument to findResource as I will be creating only one DB entry, this logic can be changed according to the requirement, like findResourceByDate, etc.).

public class ApplicationService {

private ApplicationDao applicationDao;

public ResponseEntity<byte[]> getImageURL() throws IOException {

RequestData requestData = applicationDao.findResource(1);
String imagePath = requestData.getImages();

RandomAccessFile f = new RandomAccessFile(imagePath, "r");
byte[] b = new byte[(int) f.length()];
final HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<byte[]>(b, headers, HttpStatus.CREATED);

public ResponseEntity<byte[]> getTextData() throws IOException {
RequestData requestData = applicationDao.findResource(1);
String s = requestData.getContents();

RandomAccessFile f = new RandomAccessFile(s, "r");
byte[] b = new byte[(int) f.length()];
final HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<byte[]>(b, headers, HttpStatus.CREATED);


[4] The Java class that will be mapped with MySQL Table (name can be changed as per requirement):

@Table(name = "resource_table")

public class RequestData {
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "File_id")
private String id;

@Column(name = "Images_path")
private String images;

@Column(name = "Text_path")
private String Contents;

// getter-setters


Finally, the complete project structure should look like: 

5 Complete Structure

In the application.properties, make the following changes: 

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect

spring.jpa.properties.hibernate.format_sql = true


Step 3: MySQL Changes 

I am using MySQL for demonstration. DB may change as per requirement. Below is the sample Schema, Tables, and Insertion Data:

6 Database

I have a sample text file (with some text) and image file (Spring Boot logo) at local drive(E Drive). 

Step 4: Rendering Text and Image on Browser

[1]Start the spring boot application as below: 

7 Run Spring Boot

In the logs, you are going to see: 

2018-07-16 01:29:15.513  INFO 12640 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-07-16 01:29:15.584  INFO 12640 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-07-16 01:29:15.594  INFO 12640 --- [           main] com.boot.BootAngularRestApplication      : Started BootAngularRestApplication in 3.478 seconds (JVM running for 4.488)

Tomcat started on port 8080.

[2]  On web Browser, like Chrome, type in the below URL: 

[2.1] http://localhost:8080/application/get-image     

You will get the below output: 

8 image

[2.2] http://localhost:8080/application/get-text: 

Image title

Hope this helps, thanks! 

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

spring boot ,restful api ,mysql ,tutorial ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}