提问者:小点点

在单个API调用中上传JSON数据和图像时出错:Spring Boot


我正在进行Spring Boot Project,我想在一个API调用中发送JSON数据和Multipart File(Image)。为此,我推荐-https://blogs.perficient.com/2020/07/27/requestbody-and-multipart-on-spring-boot/#:~: text=通常我们添加@Request estBody,因此,注释应该更改。
我的控制器是-

@PostMapping(value = "/create",consumes = {MediaType.APPLICATION_JSON_VALUE,MediaType.MULTIPART_FORM_DATA_VALUE})
        public ResponseEntity<SuccessResponse<PostDto>> createPost(
                @RequestPart("post") String post,
                        @RequestPart("image") MultipartFile file,
                        @RequestParam(name = "userid") Integer uid,
                        @RequestParam(name = "categoryid") Integer categoryId) {

                log.info("Filename :" + file.getOriginalFilename());
                log.info("Size:" + file.getSize());
                log.info("Contenttype:" + file.getContentType());

                //convert the post string to POJO
                 PostDto postDto=postService.getJson(post);
                //Now create the post
                 PostDto newPost = this.postService.createPost(postDto, uid, categoryId, file);
                SuccessResponse<PostDto> successResponse = new SuccessResponse<>(AppConstants.SUCCESS_CODE,
                                AppConstants.SUCCESS_MESSAGE, newPost);
                return new ResponseEntity<>(successResponse, HttpStatus.OK);
        }

当我发出请求时,我收到以下错误(注意-我已在Spring Security中设置了错误响应,如图所示。)

[nio-8085-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required part 'image' is not present.]

我尝试了另一种方法,但它给出了另一个错误-

 @PostMapping("/uploadimage/{postid}/{isUpdatingPost}")
        public ResponseEntity<SuccessResponse<String>> uploadImage(@RequestParam(name="file") MultipartFile file, @PathVariable("postid") int postid, @PathVariable("isUpdatingPost")boolean isUpdatingPost){
                String result=this.postService.uploadImage(file, postid, isUpdatingPost);
                SuccessResponse<String> response=new SuccessResponse<>(AppConstants.SUCCESS_CODE,AppConstants.SUCCESS_MESSAGE,result);
                return new ResponseEntity<>(response,HttpStatus.OK);
        }
[Request processing failed: org.springframework.web.multipart.MultipartException: Current request is not a multipart request] with root cause

org.springframework.web.multipart.MultipartException: Current request is not a multipart request

我无法理解这些方法中的问题。我还在Postman中将内容类型设置为multipart/form-data,并在@PostMaps中设置了消费参数,但仍然收到这些错误。请帮助找到问题!


共1个答案

匿名用户

这对我有用。

使用@RequestParam附加单个文件:

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> uploadImage(@RequestParam MultipartFile file) throws IOException {
    // ...
}

对于在DTO中使用@Model属性附加文件:

public record FileDTO(
        Integer id,
        MultipartFile file) {
}

@PostMapping(value = "/2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<?> uploadImage2(@ModelAttribute FileDTO dto) throws IOException {
    // ...
}

BTW,您可能希望使用OpenAPI(SwaggerUI)手动测试您的应用程序,这比使用postman更容易。

参考文章:https://www.baeldung.com/spring-file-upload