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.
- Host: GitHub
- URL: https://github.com/edgar-code-repository/spring-boot-upload-demo
- Owner: edgar-code-repository
- Created: 2025-02-10T01:16:15.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-02-17T00:08:06.000Z (over 1 year ago)
- Last Synced: 2025-05-14T02:14:42.769Z (about 1 year ago)
- Topics: backend, java17, spring-boot
- Language: Java
- Homepage:
- Size: 104 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
UPLOAD DEMO
-----------------------------------------------------------------------------------------------------
**A JSON file is received and processed in a Rest API.**

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

-----------------------------------------------------------------------------------------------------
**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:**

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