DEV Community

loading...

Upload and Read CSV File in Spring Boot

Loi Le
Software engineer
・3 min read

https://loizenai.com/upload-read-csv-spring-boot/

Tutorial: "Upload and Read CSV File in Spring Boot - SpringBoot Upload Download Multiple CSV files to MySQL/PostgreSQL with Ajax"

Creating SpringBoot RestAPIs to upload and download multiple CSV files to databases: MySQL and PostgreSQL is one of the most common question in the development world. Going through the tutorial post"SpringBoot Upload Download Multiple CSV files to MySQL/PostgreSQL", I explain details how to do it by step to step with coding and give you 100% running source code. What we will do?

  • I draw an overview diagram architecture of SpringBoot RestAPI Upload CSV Files.
  • I use Spring Web to development Spring RestApis.
  • I use ApacheCommon or Open CSV libraries to parse and read CSV files.
  • I use SpringJPA to save data from CSV files to MySQL and PostgreSQL.
  • I implement a SpringBoot Global Exception Handler when uploading with a very big files and fail.
  • I use Ajax and Bootstrap to implement a frontend client to upload/download CSV files.

Youtube Video Guide: Upload and Read CSV File in Spring Boot

https://youtu.be/n9KzlhN722s

Overview – Upload and Read CSV File in Spring Boot

Here is an overview about workflow data of SpringBoot Upload/Download multiple Csv Files project:

Overview Architecture

– We implement an Ajax or use a Rest client to upload/download Csv files to/from SpringBoot application.
– For manipulating Csv files, we develop an Csv Utils class to write and read data from them.
– We implement a Csv File Service to do CRUD operations with MySQL/PostgreSQL that supported by SpringJPA Repository.

Here is an overview of “SpringBoot Upload Download multiple CSV files to database” project that we will implement in the tutorial:

SpringBoot project structure

controller package implements Controller RestAPIs classes to upload/download Csv files from Ajax or a Rest-Client
repository package defines a CRUD JPA Repository: CustomerRepository
services package implements a class CsvFileServices with functions to store and retrieve Csv File’s data
utils package implements a class CsvUtils with common functions for reading or writing data to/from Csv files
model package defines a class Customer for mapping data between each CSV’s row with each database’s record
errorhandler package implements a class RestExceptionHandler to handle an exception: file size exceeded

Create Project – Upload and Read CSV File in Spring Boot

We use Eclipse to create a “SpringBoot upload download file” project with a set of below dependencies:

spring-boot-starter-data-jpa dependency is used to interact with PostgreSQL/MySQL database
postgresql or mysql-connector-java drivers are used to connect with PostgreSQL/MySQL database
spring-boot-starter-web dependency is used to implement Spring RestAPIs Controller
For writting and reading CSV files, we use dependency commons-csv of org.apache.commons or use dependency opencsv of com.opencsv

In the tutorial, we mainly do a demo with MySQL database, so here is the details of necessary dependencies in pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.8</version>
</dependency>       

<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>4.5</version>
</dependency>       

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
Enter fullscreen mode Exit fullscreen mode

All Tutorial At

https://loizenai.com/upload-read-csv-spring-boot/

Youtube List

https://www.youtube.com/watch?v=dTR-41_jMvc&t=46s
https://www.youtube.com/watch?v=lb5LVzJbquI&t=476s
https://www.youtube.com/watch?v=DoV8xfA8WBo&t=30s
https://www.youtube.com/watch?v=rYmf_MthobU&t=376s
https://www.youtube.com/watch?v=7ZfInOvFsz0&t=1308s

Discussion (0)