An open API service indexing awesome lists of open source software.

https://github.com/edgar-code-repository/spring-boot-upload-demo

A JSON file is received and processed in a Rest API.
https://github.com/edgar-code-repository/spring-boot-upload-demo

backend java17 spring-boot

Last synced: 12 months ago
JSON representation

A JSON file is received and processed in a Rest API.

Awesome Lists containing this project

README

          

UPLOAD DEMO
-----------------------------------------------------------------------------------------------------

**A JSON file is received and processed in a Rest API.**

![java+json](images/json+java.png)

**Each row in the file is stored in Azure SQL.**

![azuresql](images/azure-sql.png)

-----------------------------------------------------------------------------------------------------

**The file contains this data:**

```

[
{ "id": 1, "name": "Alice", "email": "alice@example.com" },
{ "id": 2, "name": "Bob", "email": "bob@example.com" },
{ "id": 3, "name": "Charlie", "email": "charlie@example.com" },
...
...
...
]

```

-----------------------------------------------------------------------------------------------------

**Endpoint that receives the file:**

```
@PostMapping
public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) {
log.debug("[uploadFile][BEGIN]");
ResponseUploadDTO responseUploadDTO = fileUploadService.uploadClientsFile(file);
log.debug("[uploadFile][END]");
return ResponseEntity.ok(responseUploadDTO);
}
```

-----------------------------------------------------------------------------------------------------

**Method that handles the JSON file:**

```
@Override
public ResponseUploadDTO uploadClientsFile(MultipartFile file) {
log.debug("[uploadClientsFile][BEGIN]");

ResponseUploadDTO responseDTO = ResponseUploadDTO.builder().build();
try {
List clients = objectMapper.readValue(file.getInputStream(), new TypeReference>() {});
clients.forEach(client -> log.debug("[uploadClientsFile][client: {}]", client));
responseDTO.setCode(101);
responseDTO.setMessage("File uploaded successfully: " + file.getOriginalFilename());
} catch (Exception e) {
responseDTO.setCode(102);
responseDTO.setMessage("Failed to upload file: " + e.getMessage());
log.error("[uploadClientsFile][error: {}]", e.toString());
}

log.debug("[uploadClientsFile][END]");
return responseDTO;
}
```

-----------------------------------------------------------------------------------------------------

**The endpoint is executed using Postman:**

![postman](screenshots/postman.png)

-----------------------------------------------------------------------------------------------------