Я протестировал через Postman приложение и получил следующее предупреждение:
"org.springframework.http.converter.HttpMessageNotWritableException: не найдено преобразователя для возвращаемого значения типа: class sun.nio.ch.ChannelInputStream "
может кто знает, как решить эту проблему?
код представлен ниже
Мой контроллер
@RestController
public class FileServiceController {
private FileService fileService;
@Autowired
public FileServiceController(FileService fileService) {
this.fileService = fileService;
}
@CrossOrigin
@RequestMapping(value = "/api/v1/write")
public ResponseEntity writeToFile(@RequestParam final String sessionId, @RequestParam final String path) throws FileServiceException {
return path != null ? new ResponseEntity<>(fileService.openForWriting(sessionId, path),
HttpStatus.OK) : new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
@CrossOrigin
@RequestMapping(value = "/api/v1/files")
public ResponseEntity getFiles( @RequestParam final String sessionId, @RequestParam final String path) throws FileServiceException {
return path != null ? new ResponseEntity<>(fileService.getFiles(sessionId, path), HttpStatus.FOUND) :
new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
@CrossOrigin
@RequestMapping(value = "/api/v1/read")
public ResponseEntity readFromFile(@RequestParam final String sessionId, @RequestParam final String path) throws FileServiceException {
return path != null ? new ResponseEntity<>(fileService.openForReading(sessionId, path), HttpStatus.FOUND) :
new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
@CrossOrigin
@RequestMapping(value = "/api/v1/delete")
public ResponseEntity deleteFromFile(@RequestParam final String sessionId, @RequestParam final String path) throws FileServiceException {
return path != null ? new ResponseEntity<>(fileService.delete(sessionId, path), HttpStatus.OK) :
new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
Мой FileServiceImpl
@Service
public class FileServiceImpl implements FileService {
@Override
public OutputStream openForWriting(final String sessionId, final String path) throws FileServiceException {
try {
return Files.newOutputStream(Paths.get(path), StandardOpenOption.APPEND);
} catch (final IOException e) {
throw new FileServiceException("cannot open entry", e);
}
}
@Override
public InputStream openForReading(final String sessionId, final String path) throws FileServiceException {
try {
return Files.newInputStream(Paths.get(path));
} catch (final IOException e) {
throw new FileServiceException("cannot open entry", e);
}
}
@Override
public List<String> getFiles(final String sessionId, final String path) throws FileServiceException {
try (Stream<Path> paths = Files.walk(Paths.get(path))) {
return paths.filter(Files::isRegularFile)
.map(Path::toString)
.collect(Collectors.toList());
} catch (IOException e) {
throw new FileServiceException("cannot get files", e);
}
}
@Override
public boolean delete(final String sessionId, final String path) throws FileServiceException {
Path rootPath = Paths.get(path);
try {
Files.walk(rootPath)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} catch (IOException e) {
throw new FileServiceException("cannot delete entries", e);
}
return true;
}
}
Интерфейс
public interface FileService {
@NotNull OutputStream openForWriting(@NotNull final String sessionId, final String path) throws FileServiceException;
@NotNull InputStream openForReading(@NotNull final String sessionId, final String path) throws FileServiceException;
@NotNull List<String> getFiles(@NotNull final String sessionId, final String path) throws FileServiceException;
boolean delete(@NotNull final String sessionId, final String path) throws FileServiceException;
}
Приложение
@SpringBootApplication
public class FileServiceApplication {
public static void main(final String[] args) {
SpringApplication.run(FileServiceApplication.class, args);
}
}