Spring boot 如何使用@RequestBody在Spring控制器中上载文件?
我正在使用Angular开发弹簧靴。 我试图在一个请求中上载多个文件以及其他属性 例如,我有一个产品,里面有不同的产品。两者都有画廊图片 我提交的基础产品,其中包含变种以及 下面是控制器端提交的示例对象Spring boot 如何使用@RequestBody在Spring控制器中上载文件?,spring-boot,spring-mvc,Spring Boot,Spring Mvc,我正在使用Angular开发弹簧靴。 我试图在一个请求中上载多个文件以及其他属性 例如,我有一个产品,里面有不同的产品。两者都有画廊图片 我提交的基础产品,其中包含变种以及 下面是控制器端提交的示例对象 public class ProductDTO { private String name; private String code; private String size; private List<MultipartFile> mu
public class ProductDTO {
private String name;
private String code;
private String size;
private List<MultipartFile> multimedias;
private List<ProductDTO> variants;
}
公共类产品到{
私有字符串名称;
私有字符串码;
私有字符串大小;
私有列表多媒体;
私有列表变体;
}
控制器代码如下
@PostMapping
public ResponseDTO<ProductDTO> createProduct(@RequestBody ProductDTO productDTO, Authentication authentication, HttpServletResponse response) throws Exception {
return tryCreateItem(() -> {
productService.createOrUpdateProduct(productDTO, true);
return productDTO;
}, response);
}
@PostMapping
public ResponseDTO createProduct(@RequestBody ProductDTO ProductDTO,Authentication Authentication,HttpServletResponse)引发异常{
返回tryCreateItem(()->{
createOrUpdateProduct(productDTO,true);
将产品返回给客户;
},回应);
}
在角侧,提交产品和变体以及图库图像。
但是我没有得到控制器中的文件
有人能帮我解决我的问题吗
如何将表单中的文件作为RequestBody对象上载?
我正在标记Angularjs,希望任何人都会遇到这种情况,或者angular侧需要任何特定的配置。@PostMapping
@PostMapping
public ResponseDTO<ProductDTO> createProduct(@RequestBody ProductDTO productDTO, @RequestParam("files") MultipartFile[] files, Authentication authentication, HttpServletResponse response) throws Exception {
if(null!=files){
for(int i=0;i<files.length();i++){
multimedias.add(files[0]);
}
}
return tryCreateItem(() -> {
productService.createOrUpdateProduct(productDTO, true);
return productDTO;
}, response);
}
public class ProductDTO {
private String name;
private String code;
private String size;
@transient
private List<MultipartFile> multimedias=new ArrayList<MultipartFile>();
private List<ProductDTO> variants;
public ResponseDTO createProduct(@RequestBody ProductDTO ProductDTO,@RequestParam(“文件”)MultipartFile[]文件,身份验证,HttpServletResponse)引发异常{
if(null!=文件){
对于(int i=0;i{
createOrUpdateProduct(productDTO,true);
将产品返回给客户;
},回应);
}
公共类产品{
私有字符串名称;
私有字符串码;
私有字符串大小;
@短暂的
私有列表多媒体=新的ArrayList();
私有列表变体;
}你检查了吗?是的@Aboullite已经阅读了这么多类似的文章。没有,我在Rest controller中找到了以表单形式发送的文件。使用请求参数可以,但不适合我的要求。我无法基于不同的对象获取文件。该问题标记为AngularJS,但没有AngularJS代码。