StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 8 · File Handling and Storage

CSV Bulk Import Handler

medium—No data yet

Problem statement

Implement a bulk product import that reads an uploaded CSV file, stores its valid rows, and reports each failing row with a reason.

Requirements

ImportService

The service owns the import; implement its method:

  • importCsv receives the uploaded file, processes it row by row as described below, stores every valid row as a product, and returns the import summary

ImportController

The controller exposes one endpoint; it must be reachable at exactly the verb and path listed below.

POST /products/import

  • Accept a multipart form upload carrying the CSV file in a form field named file
  • If the request carries no file field, return 400 Bad Request with error message: "File is required" and store nothing
  • The first line of the file is the header line and is never imported. Every line after it is a data row; data rows are numbered starting at 1. Empty lines at the end of the file are ignored; an empty line anywhere else counts as a data row.
  • A data row consists of comma-separated values in the order name, price, quantity
  • Check each data row against the rules below in order; the first failing rule decides the row's reason and each failing row is reported exactly once
  • A row that does not have exactly three values fails with reason "Wrong number of columns"; an empty line in the middle of the file fails this rule
  • If the name value is empty or contains only spaces, the row fails with reason "Name must not be blank"
  • If the price value is not a whole number greater than zero, the row fails with reason "Price must be a positive integer"; a price of exactly 1 is valid and a price of 0 is not
  • If the quantity value is not a whole number of zero or greater, the row fails with reason "Quantity must be a non-negative integer"; a quantity of exactly 0 is valid and a quantity of -1 is not
  • Store every valid row as a product with the row's name, its price stored in priceCents, and its quantity, even when other rows of the same file fail; a failing row stores nothing
  • Importing never changes or removes products that are already stored; imported rows are added alongside them
  • Return 200 OK with a summary made of exactly imported, the number of rows stored, and failed, a list with one entry per failing row ordered by row ascending; each entry of failed is made of exactly the row number and the reason
  • When every data row is valid, failed is an empty list; an empty file or a file holding only the header line returns 200 OK with imported 0 and an empty failed list

Sample Requests & Responses

Expand to view details on sample requests and responses.

POST /products/import

Request (multipart form, field file holding this CSV file):

name,price,quantity
Desk Lamp,1999,5
Mouse Pad,499,12

Response (200 OK):

{
   "imported": 2,
   "failed": []
}

POST /products/import

Request (multipart form, field file holding this CSV file):

name,price,quantity
Notebook,450,10
,300,2
Marker,120,25
Stapler,0,4
Tape,150,-2

Response (200 OK):

{
   "imported": 2,
   "failed": [
      {
         "row": 2,
         "reason": "Name must not be blank"
      },
      {
         "row": 4,
         "reason": "Price must be a positive integer"
      },
      {
         "row": 5,
         "reason": "Quantity must be a non-negative integer"
      }
   ]
}

POST /products/import

Request (multipart form, field file holding this CSV file):

name,price,quantity

Response (200 OK):

{
   "imported": 0,
   "failed": []
}

POST /products/import

Request: a multipart form without a file field.

Response (400 Bad Request):

{
   "error": "File is required",
   "timestamp": "2026-08-05T21:48:04.686021",
   "path": "/products/import"
}

More in File Handling and Storage

See the module
Passport Photo UploadThis problem covers accepting an uploaded image, checking it before storing it, and serving the original bytes back later.—No data yetmediumLecture Slide RepositoryThis problem covers storing uploaded PDF decks under a course and sending them back as a download under their original name.—No data yetmediumScanned Receipt VaultThis problem covers storing uploaded receipts per user, cleaning up the filename and holding each user to a storage limit.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.