9

I have created repository like given code

@RepositoryRestResource(collectionResourceRel = "sample", path = "/sample" )
public interface SampleRepository extends PagingAndSortingRepository<Sample, Long> {

}

works fine for allcrud operations.

But I wanted to create a rest repository which upload file, How i would do that with spring-data-rest?

Manish Carpenter
  • 167
  • 1
  • 2
  • 11

2 Answers2

10

Spring Data Rest simply exposes your Spring Data repositories as REST services. The supported media types are application/hal+json and application/json.

The customizations you can do to Spring Data Rest are listed here: Customizing Spring Data REST.

If you want to perform any other operation you need to write a separate controller (following example from Uploading Files):

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUploadController {

    @RequestMapping(value="/upload", method=RequestMethod.GET)
    public @ResponseBody String provideUploadInfo() {
        return "You can upload a file by posting to this same URL.";
    }

    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
            @RequestParam("file") MultipartFile file){
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(new File(name)));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + "!";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }

}
Francesco Pitzalis
  • 1,922
  • 14
  • 20
  • I am accepting you answer. But its part of spring-mvc. What about – Manish Carpenter Aug 19 '15 at 10:30
  • ... What about RepositoryRestController instead of using Controller? – Manish Carpenter Aug 19 '15 at 10:49
  • 2
    You should use `@RepositoryRestController` explicitly only if you want to override the default behaviour of the Spring Data REST auto generated controllers. For more information have a look [here](http://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.overriding-sdr-response-handlers). – Francesco Pitzalis Aug 19 '15 at 13:40
  • Did you simply copy paste your answer from [this](http://stackoverflow.com/a/25890581/3640307) previous question? If you think this question already had an answer, you should have marked it that way, instead of copy pasting the answer! – Sajib Acharya Oct 15 '16 at 20:23
  • @SajibAcharya I did not. The questions are about two different things and I didn't see that answer simply because of that. The code example in my answer is taken from the link I provided (from Spring's official guides), not from the other answer where I guess he copy/pasted from the same source without mentioning it. Thanks for the contribution. – Francesco Pitzalis Oct 17 '16 at 08:27
4

Yes you can try this:

@RestController
@EnableAutoConfiguration
@RequestMapping(value = "/file-management")
@Api(value = "/file-management", description = "Services for file management.")
public class FileUploadController {
    private static final Logger LOGGER = LoggerFactory
            .getLogger(FileUploadController.class);
    @Autowired
    private StorageService storageService;  //custom class to handle upload.
    @RequestMapping(method = RequestMethod.POST, headers = ("content-    type=multipart/*"), produces = "application/json", consumes =            MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseBody
    @ResponseStatus(value = HttpStatus.CREATED)
    public void handleFileUpload(
            @RequestPart(required = true) MultipartFile file) {
        storageService.store(file);  //your service to hadle upload.
    }
}