Разобрать/сохранить файл с Spring Server. Я пытаюсь достичь конечных точек в PostmanJAVA

Программисты JAVA общаются здесь
Anonymous
Разобрать/сохранить файл с Spring Server. Я пытаюсь достичь конечных точек в Postman

Сообщение Anonymous »

Вот мой класс контроллера:

Код: Выделить всё

package com.example.justin_fulkerson_project.controllers;// FileController.java
import com.example.justin_fulkerson_project.entities.MapDataEntity;
import com.example.justin_fulkerson_project.respositories.MapDataRepository;
import com.example.justin_fulkerson_project.services.FileParserService;
import com.example.justin_fulkerson_project.util.FixedLengthField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/files")
public class FileController {

@Autowired
private FileParserService fileParserService;

@Autowired
private MapDataRepository mapDataRepository;

private String saveUploadedFile(MultipartFile file) throws IOException {
// Generate a unique filename
String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();

// Define the directory where the file will be saved
String uploadDir = "uploads";

// Create the upload directory if it doesn't exist
File directory = new File(uploadDir);
if (!directory.exists()) {
directory.mkdir();
}

// Save the file to the upload directory
Path filePath = Paths.get(uploadDir, fileName);
Files.copy(file.getInputStream(), filePath);

// Return the absolute path of the saved file
return filePath.toAbsolutePath().toString();
}

@PostMapping("/parse")
public List parseFile(@RequestParam("file") MultipartFile file, @RequestBody Map fields) throws IOException {
String filePath = saveUploadedFile(file); // Save the uploaded file and get its path
return fileParserService.parseFixedLengthFile(filePath, fields);
}

@PostMapping("/save")
public void saveParsedData(@RequestBody List parsedData) {
for (Map record : parsedData) {
MapDataEntity mapDataEntity = new MapDataEntity();
mapDataEntity.setData(record);
mapDataRepository.save(mapDataEntity);
}
}

// Implement other endpoints as needed
}

Я получаю следующую ошибку:
{
"timestamp": "2024-03-16T19:44:49.636+00 :00",
"статус": 415,
"ошибка": "Неподдерживаемый тип носителя",
"путь": "/api/files/save"
}< /p>
Это связано с данными в моем файле? Я пытаюсь достичь конечной точки с помощью данных json.
Это данные в моем файле:
[
{
"firstName": "Джастин",
"lastName": "Фалкерсон"
},
{
"firstName": "Джон",
"lastName" : "Доу"
}
]
Буду признателен за любую помощь.
Я пробовал загрузить оба данных в формате .txt. и данные .json.

Подробнее здесь: https://stackoverflow.com/questions/781 ... in-postman

Вернуться в «JAVA»