有没有办法在不使用 Spring-MVC 的情况下使用 spring-data-rest 编写一个 rest 控制器来上传文件?
Is there a way to write a rest controller to upload file using spring-data-rest without using Spring-MVC?
我已经按照给定的代码创建了存储库
@RepositoryRestResource(collectionResourceRel = "sample", path = "/sample" )
public interface SampleRepository extends PagingAndSortingRepository<Sample, Long> {
}
适用于所有 crud 操作。
但我想创建一个上传文件的休息存储库,
我将如何使用 spring-data-rest 来做到这一点?
Spring Data Rest 只是将您的 Spring 数据存储库公开为 REST 服务。支持的媒体类型是 application/hal+json
和 application/json
。
此处列出了您可以对 Spring 数据 Rest 进行的自定义:Customizing Spring Data REST。
如果你想执行任何其他操作,你需要编写一个单独的控制器(以下示例来自 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.";
}
}
}
是的,你可以试试这个:
@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.
}
}
我已经按照给定的代码创建了存储库
@RepositoryRestResource(collectionResourceRel = "sample", path = "/sample" )
public interface SampleRepository extends PagingAndSortingRepository<Sample, Long> {
}
适用于所有 crud 操作。
但我想创建一个上传文件的休息存储库, 我将如何使用 spring-data-rest 来做到这一点?
Spring Data Rest 只是将您的 Spring 数据存储库公开为 REST 服务。支持的媒体类型是 application/hal+json
和 application/json
。
此处列出了您可以对 Spring 数据 Rest 进行的自定义:Customizing Spring Data REST。
如果你想执行任何其他操作,你需要编写一个单独的控制器(以下示例来自 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.";
}
}
}
是的,你可以试试这个:
@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.
}
}