0

I have a REST web service realized through Spring, it returns an object Response with 4 field, so the constructor is :

Response(boolean status, boolean success, Object result, ErrorResponse error)

Below there is the web service:

@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);
        }       
    }

In Object field of Response I would like tu fill File, so when I call this web service I can retrieve the file from the server. But in my client application the Response result field is a String and not a File.

@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;
}

Do you know where is the error? My aim is to send file from server and receive and store it in another pc. Thanks, regards Otherwise, if I use

@RequestMapping(value = "/{path}", method = RequestMethod.GET)
public @ResponseBody FileSystemResource getFile(@PathVariable("path") String path) {
    return new FileSystemResource(matlabClientServices.getFile(path)); 
}

how can retrieve the file and write it in specific path and how can I check for exception or other error?

luca
  • 3,248
  • 10
  • 66
  • 145

1 Answers1

0

Sending a binary file from a Spring controller can't be done simply by assigning the file to a bean and returning that bean! Most likely Spring's content negotiation will serialize you bean to XML or Json when dispatching the HTTP response, which won't include the file's data.

If your file contains string data, you can try to make it part of that XML or JSON by reading it and assigning it to a string property.

If on the other hand, the file contains binary data, e.g. a video or an image, your will have to send it on its own along the lines that are indicated here: Downloading a file from spring controllers (scroll down a bit for the @ResponseBody solution).

Community
  • 1
  • 1
Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60