我通过Postman测试了该应用程序并收到以下警告:
“ org.springframework.http.converter.HttpMessageNotWritableException:未找到类型为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);
}
}
答案 0 :(得分:0)
FileServiceImpl.openForReading()
返回一个InputStream
,这就是您在FileServiceController.readFromFile()
中输入的内容。 InputStream
不可序列化,因此是例外。
您应该放置从其中读取的内容而不是InputStream
,例如byte[]
,字符串或应用程序要处理的任何对象。