我有一个@RestController
,它允许使用一些@RequestParam
参数和2个@RequestPart
参数进行POST,以便能够创建一个服务层中的对象需要一些输入 + 2 个文件,如下所示:
@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity createBook(@RequestParam("authorId") UUID authorId,
@RequestParam("pages") int pages,
@RequestParam("rating") BookRating rating,
@RequestPart("file") MultipartFile file,
@RequestPart("file2") MultipartFile file2) {
我想创建一个封装所有这些参数的对象,这样我就可以执行以下操作:
public ResponseEntity createBook( CreateBookParameters params ) {
我该怎么做?我需要在 Controller 中使用什么注释?我需要在 CreateBookParameters
中使用任何注释吗?还是这是一个简单的 POJO?
如果我进行这些更改,我是否需要对我所做的请求进行任何更改(我正在使用 Postman 中的“表单数据”设置进行测试)?
最佳答案
你尝试过Spring的默认反射行为吗?试试这个:
public class CreateBookParameters {
private UUID authorId;
private int pages;
private BookRating rating;
private MultipartFile file;
private MultipartFile file2;
// add getters & setters for each
}
@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity createBook(CreateBookParameters p) {
// ...
}
关于java - 在 Spring Boot Controller 中使用对象代替多个参数和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34726103/