我有一个通过Spring实现的REST Web服务,它返回一个带有4个字段的对象Response,所以构造函数是:
Response(boolean status, boolean success, Object result, ErrorResponse error)
下面是网络服务:
@Override
@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody Response getAcquisition(@RequestParam(value="path", defaultValue="/home") String path){
File file;
try {
file = matlabClientServices.getFile(path);
if (file.exists())
return new Response(true, true, file, null);
else
return new Response(false, false, "File doesn't exist!", null);
} catch (Exception e) {
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in MatlabClientControllerImpl::getAcquisition :" + errorResponse.getStacktrace());
return new Response(false, false, "Error during file retrieving!", errorResponse);
}
}
在Response的Object字段中我想要填充文件,所以当我调用这个Web服务时,我可以从服务器中检索文件。 但在我的客户端应用程序中,响应结果字段是字符串而不是文件。
@Override
@RequestMapping(value = "/test/", method = RequestMethod.GET)
public Response getFileTest(@RequestParam(value="path", defaultValue="/home") String path){
RestTemplate restTemplate = new RestTemplate();
Response response = restTemplate.getForObject("http://localhost:8086/ATS/client/file/?path={path}", Response.class, path);
if (response.isStatus() && response.isSuccess()){
@SuppressWarnings("unused")
File fileLoaded= (File)response.getResult();
}
return response;
}
你知道错误在哪里吗?我的目标是从服务器发送文件并接收并存储在另一台PC中。 感谢和问候 否则,如果我使用
@RequestMapping(value = "/{path}", method = RequestMethod.GET)
public @ResponseBody FileSystemResource getFile(@PathVariable("path") String path) {
return new FileSystemResource(matlabClientServices.getFile(path));
}
如何检索文件并将其写入特定路径,如何检查异常或其他错误?