So I am trying to send a file and an object to my Backend. I then use the Multipartfile to create an Apache Poi Workbook. Using apache's Iterator I loop over the file, extracting and saving what I need. That is working well (it even is saving the new data in the database), however the function when reaching the Return statement is not returning.
It seems to not terminate, since it is not returning a StatusCode, or an object (tried different Options, see below). My other controlerfunctions are doing fine, so I ensured, that my Code is reaching the return statement. I took lines out of the code until the problem is no longer occurring. It seems to occur when creating the workbook. I tried closing the Workbook. I even tried closing the Inputstream manually but the error is still occurring. I tried changing the return value (returning a Responseentity, returning a DTO, having a void function, and setting @HTTPResponseStatus), but that didn't change the behavior. When I took out the workbook all three methods worked. I tried different versions of apache poi (5.0.0, 4.0.0, 3.15). But still no return value.
This is my Controller:
@RestController
@RequestMapping("/trafficdata")
public class TrafficDataController {
@Autowired
TrafficDataRepository trafficDataRepository;
@PostMapping(value = "/filedata", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity addDataFromFile(@RequestParam("model") String model, @RequestParam(value = "file", required = false) MultipartFile file) throws PermissionDeniedException, FileNotImportableException, DataKeyAlreadyTakenException, JsonProcessingException {
UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if(!userDetails.isProfessor()){
throw new PermissionDeniedException();
}
ObjectMapper mapper = new ObjectMapper();
FileTrafficDataInputDTO trafficDataInputDTO = mapper.readValue(model, FileTrafficDataInputDTO.class);
if(trafficDataRepository.existsByDataKey(trafficDataInputDTO.getDataKey())){
throw new DataKeyAlreadyTakenException(trafficDataInputDTO.getDataKey());
}
Workbook workbook;
DataFormatter dataFormatter = new DataFormatter();
String lowerCaseFileName = file.getOriginalFilename().toLowerCase();
InputStream inputStream;
try {
inputStream = file.getInputStream();
workbook = WorkbookFactory.create(inputStream); //as soon as I take out this, it works
} catch (IOException e) {
e.printStackTrace();
throw new FileNotImportableException(file.getOriginalFilename());
}
try {
//I tried something here inputStream.close();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
/* working with the data */
return new ResponseEntity(HttpStatus.CREATED);
}}
these are my Maven dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.directory.api</groupId>
<artifactId>api-all</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
Does anyone knows how to solve the issue and/or why exactly the issue is occuring?