我通过邮递员测试了应用程序并收到以下警告:
"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);
}
}
My 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);
}
}
FileServiceImpl.openForReading()
返回一个InputStream
,这就是您在FileServiceController.readFromFile()
中的响应中输入的内容。InputStream
不可序列化,因此存在异常。
而不是InputStream
,你应该把你从中读取的内容,例如byte[]
,字符串或你的应用程序处理的任何对象。