Spring Boot Starter CSV

Spring Boot starter for importing CSV.

License

License

Categories

Categories

Spring Boot Container Microservices CSV Data Data Formats
GroupId

GroupId

nl.42
ArtifactId

ArtifactId

spring-boot-starter-csv
Last Version

Last Version

1.1.5
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Boot Starter CSV
Spring Boot starter for importing CSV.
Source Code Management

Source Code Management

https://github.com/42BV/spring-boot-starter-csv

Download spring-boot-starter-csv

How to add to project

<!-- https://jarcasting.com/artifacts/nl.42/spring-boot-starter-csv/ -->
<dependency>
    <groupId>nl.42</groupId>
    <artifactId>spring-boot-starter-csv</artifactId>
    <version>1.1.5</version>
</dependency>
// https://jarcasting.com/artifacts/nl.42/spring-boot-starter-csv/
implementation 'nl.42:spring-boot-starter-csv:1.1.5'
// https://jarcasting.com/artifacts/nl.42/spring-boot-starter-csv/
implementation ("nl.42:spring-boot-starter-csv:1.1.5")
'nl.42:spring-boot-starter-csv:jar:1.1.5'
<dependency org="nl.42" name="spring-boot-starter-csv" rev="1.1.5">
  <artifact name="spring-boot-starter-csv" type="jar" />
</dependency>
@Grapes(
@Grab(group='nl.42', module='spring-boot-starter-csv', version='1.1.5')
)
libraryDependencies += "nl.42" % "spring-boot-starter-csv" % "1.1.5"
[nl.42/spring-boot-starter-csv "1.1.5"]

Dependencies

compile (6)

Group / Artifact Type Version
org.apache.commons : commons-lang3 jar
com.google.guava : guava jar 30.1-jre
org.csveed : csveed jar 0.5.0
com.github.albfernandez : juniversalchardet jar 2.4.0
org.springframework.boot : spring-boot-starter-security jar
org.springframework.boot : spring-boot-starter-web jar

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar

test (2)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar
org.junit.vintage : junit-vintage-engine jar

Project Modules

There are no modules declared in this project.

Spring Boot Starter CSV

Provides support for importing data by CSV files. This Spring Boot starter registers a CsvService and /csv endpoint, capable of handling CSV files.

It's also possible to import CSV files using a file directory.

Handler

Each type of CSV file demands a handler implementation. This handler describes how the file should be parsed and handled. Register a component that implements the CsvHandler interface:

@Slf4j
@Component
public class OrderCsvHandler implements CsvHandler<OrderCsvRow> {

  public static final String TYPE = "orders";

  @Override
  public String getType() {
    return TYPE;
  }

  @Override
  public Class<OrderCsvRow> getBeanClass() {
    return OrderCsvRow.class;
  }

  @Override
  public CsvResult handle(CsvClient<OrderCsvRow> client) {
    return new CsvTemplate().read(client::readBean, (order) -> save(order));
  }

}

Handlers are detected during application startup and provided to end users. When handlers should be protected, please add your own security logic.

Endpoints

GET /csv

Retrieve all registered CSV types and default separator/quote characters. This endpoint is used to prefill the CSV upload form.

POST /csv

Processes the CSV file based on the following required parameters:

  • file: CSV file (multipart)
  • type: CSV type name, as specified in handler
  • separator: Separator character
  • quote: Quote character

Content will be processed per line and results are returned as response body:

{
 "success": 10,
 "errors": [
  {
   "rowNumber": 2,
   "message": "Could not map column 'age' at index 4: For input string: 'not a number'"
  }
 ]
}

File

Files can also be uploaded via the file system. Enabling file mode requires some configuration:

csv:
  file:
    cron: '*/5 * * * *' (cron when to scan)
    directory: /opt/app/csv (base directory)
    run_on_startup: true (scan on startup, default false)

In file mode the application will automatically scan the file system for new *.csv files in the upload directory of every registered handler and attempt to process them.

Files are moved to the work directory and processed. After processing it will either move to the success or fail directory. Error messages are moved in the logs directory.

The directory structure looks as follows:

 /opt/app/csv (base dir)
  /persons (csv type)
   /uploads (inbox)
   /work
   /success
   /fail
   /logs
  /orders (other type)

Make sure the user running the application has write access to the base directory. Sub directories will be created on demand.

nl.42

Versions

Version
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
0.3.0
0.2.0
0.1.2
0.1.1
0.1.0
0.0.1