Search code examples
springspring-bootresttemplate

How to download file using Spring boot Http Interface client


I want to use HttpExchage in my upcoming project. I read many articles for HTTP interface clients. Everywhere the Object was returned as part of the return type. I want to use upload and download files via HTTP Interface. Is there any way to upload/download the file?

interface RepositoryService {

    @GetExchange("/repos/{owner}/{repo}")
    Repository getRepository(@PathVariable String owner, @PathVariable String repo);

    // more HTTP exchange methods...

}

Solution

  • Use the same classes as you would use in the controller methods for upload and download, namely Resource and MultipartFile.

    import org.springframework.core.io.Resource;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.RequestPart;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.service.annotation.GetExchange;
    import org.springframework.web.service.annotation.PostExchange;
    
    interface MyClient {
    
      @GetExchange("/download")
      Resource download();
    
      @PostExchange("/upload")
      ResponseEntity<?> upload(@RequestPart("file") MultipartFile file);
    
    }