in my spring boot application I have created a REST API POST method that read a file csv in a folder and than save on db in this way:
@Override
public ResponseList updateDataFromCSV(CsvDataInputBean csvDataInputBean)
throws IllegalStateException, FileNotFoundException {
final String methodName = String.format("updateDataFromCSV %s", csvDataInputBean);
try {
startLog(methodName, csvDataInputBean);
ResponseList response = new ResponseList();
log.info("Input path " + csvDataInputBean.getPath().toString());
List<CsvUserPojo> csvUserFile = new CsvToBeanBuilder<CsvUserPojo>(
new FileReader(csvDataInputBean.getPath())).withType(CsvUserPojo.class).withSeparator(';').build()
.parse();
List<UserEntity> listToSave = new LinkedList<>();
List<UserModel> listOfModels = new LinkedList<>();
for (CsvUserPojo csvUser : csvUserFile) {
log.info("User present in CSV file: " + csvUser);
UserEntity userEntity = new UserEntity();
Optional<UserEntity> userByCF = userRepository.findByCf(csvUser.getCf().toUpperCase());
if (!userByCF.isPresent()) {
userEntity.setName(Arrays.stream(csvUser.getName().split(" "))
.map(word -> word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase())
.collect(Collectors.joining(" ")));
userEntity.setSurname(Arrays.stream(csvUser.getSurname().split(" "))
.map(word -> word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase())
.collect(Collectors.joining(" ")));
userEntity.setGender(StringUtils.capitalize(csvUser.getGender()));
userEntity.setAge(csvUser.getAge());
userEntity.setCf(csvUser.getCf().toUpperCase());
userEntity.setAddress(Arrays.stream(csvUser.getAddress().split(" "))
.map(word -> word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase())
.collect(Collectors.joining(" ")));
userEntity.setMail(csvUser.getMail());
listToSave.add(userEntity);
UserModel userModel = ConvertersUtils.createUserModelFromUserEntity(userEntity);
listOfModels.add(userModel);
log.info("User saved on DB: " + userEntity.getCf().toUpperCase());
} else {
log.info("User with CF: " + userByCF.get().getCf().toUpperCase() + " already present on DB");
}
}
if (listToSave.isEmpty()) {
endLog(methodName, listToSave, LogConstant.LOG_GENERIC_ERROR);
response.setMessage(LogConstant.LOG_GENERIC_ERROR + " No new Users have been added");
response.setTotalResult(listOfModels.size());
response.setUsers(listOfModels);
return response;
} else {
userRepository.saveAll(listToSave);
endLog(methodName, listToSave, LogConstant.LOG_SUCCESS);
response.setMessage(LogConstant.LOG_SUCCESS + " " + listToSave.size() + " Users have been added");
response.setTotalResult(listOfModels.size());
response.setUsers(listOfModels);
return response;
}
} catch (final Exception e) {
this.errorLog(methodName, e);
throw e;
}
}
If I try to use it in local I have no problem and STS read the file csv on the folder .../Downloads/User.csv.
I have also created a file docker-compose.yml like this:
version: '3.9'
services:
#new service (java_app)
java_app:
container_name: java_app
image: java_app:1.0.0
build: .
ports:
- 8000:8080
environment:
- DATABASE_URL=jdbc:postgresql://java_db:5432/testuserdb
- DATABASE_USERNAME=postgres
- DATABASE_PASSWORD=postgres
depends_on:
- java_db
#old service (postgres)
java_db:
container_name: java_db
image: postgres:12
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: testuserdb
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata: {}
When I try to execute the POST API REST, with the same path, in the log of docker desktop I see:
java.io.FileNotFoundException: C:\Users\PastoreGu\Downloads\User1.csv (No such file or directory)
How can I solve it?