java 有没有办法在不使用Spring-MVC的情况下编写一个rest控制器来使用spring-data-rest上传文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32067097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:34:38  来源:igfitidea点击:

Is there a way to write a rest controller to upload file using spring-data-rest without using Spring-MVC?

javaspringspring-data-rest

提问by Manish

I have created repository like given code

我已经像给定的代码一样创建了存储库

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

}

works fine for allcrud operations.

适用于 allcrud 操作。

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

但是我想创建一个上传文件的休息存储库,我将如何使用 spring-data-rest 来做到这一点?

回答by Francesco Pitzalis

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

Spring Data Rest 只是将您的 Spring Data 存储库公开为 REST 服务。支持的媒体类型是application/hal+jsonapplication/json

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

此处列出了您可以对 Spring Data Rest 进行的自定义Customizing Spring Data REST

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

如果您想执行任何其他操作,您需要编写一个单独的控制器(以下示例来自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.";
        }
    }

}

回答by Ravindra Kumar

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.
    }
}