如何在Spring中在同一个函数中处理对象和多部分文件?

huangapple go评论61阅读模式
英文:

How to take Objecta nd Multipart file in same function in Spring?

问题

I am trying to create a controller function that takes Post requests to the /user. When testing on Postman I get org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'multipart/form-data;. The function takes 2 objects, one I created and a MultiPartFile object, what Should I do?

UserService Controller, Post Function

    @PostMapping("/user")
    public ResponseEntity<?> save(@RequestBody UserSignUpDto user, @RequestParam("inpFile") MultipartFile file, BindingResult bindingResult) {
        System.out.println("Hi there 1");

        System.out.println(user.getEmail());
        System.out.println(user.getUsername());

        if (userService.doesUserExist(user)) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Username already exists.");
        }

        userService.setUserProfilePicture(user, file);
        User savedUser = userService.addUser(user);

        return ResponseEntity.status(HttpStatus.CREATED).body(savedUser);
    }

Postman:
如何在Spring中在同一个函数中处理对象和多部分文件?

英文:

I am trying to create a controller function that takes Post requests to the /user. When testing on Postman I get org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type &#39;multipart/form-data;. The function takes 2 objects, one I created and a MultiPartFile object, what Should I do?

UserService Controller, Post Function

    @PostMapping(&quot;/user&quot;)
    public ResponseEntity&lt;?&gt; save(@RequestBody UserSignUpDto user, @RequestParam(&quot;inpFile&quot;) MultipartFile file, BindingResult bindingResult) {
        System.out.println(&quot;Hi there 1&quot;);

        System.out.println(user.getEmail());
        System.out.println(user.getUsername());

        if (userService.doesUserExist(user)) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, &quot;Username already exists.&quot;);
        }

        userService.setUserProfilePicture(user, file);
        User savedUser = userService.addUser(user);

        return ResponseEntity.status(HttpStatus.CREATED).body(savedUser);
    }

Postman:
如何在Spring中在同一个函数中处理对象和多部分文件?

答案1

得分: 1

你应该使用@RequestPart注解来标注这两个变量。例如:

@PostMapping("/user")
public ResponseEntity<?> save(@RequestPart UserSignUpDto user, @RequestPart("inpFile") MultipartFile file, BindingResult bindingResult) {
   System.out.println("Hi there 1");

   System.out.println(user.getEmail());
   System.out.println(user.getUsername());

   if (userService.doesUserExist(user)) {
      throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Username already exists.");
   }

   userService.setUserProfilePicture(user, file);
   User savedUser = userService.addUser(user);

   return ResponseEntity.status(HttpStatus.CREATED).body(savedUser);
}

我不知道BindingResult是什么,所以我没有修改它。你可以在这里的第三节中找到关于它的简要信息。

英文:

You should annotate both variables with @RequestPart annotation. For example:

@PostMapping(&quot;/user&quot;)
public ResponseEntity&lt;?&gt; save(@RequestPart UserSignUpDto user, @RequestPart(&quot;inpFile&quot;) MultipartFile file, BindingResult bindingResult) {
   System.out.println(&quot;Hi there 1&quot;);

   System.out.println(user.getEmail());
   System.out.println(user.getUsername());

   if (userService.doesUserExist(user)) {
      throw new ResponseStatusException(HttpStatus.BAD_REQUEST, &quot;Username already exists.&quot;);
   }

   userService.setUserProfilePicture(user, file);
   User savedUser = userService.addUser(user);

   return ResponseEntity.status(HttpStatus.CREATED).body(savedUser);
}

I don't know what is BindingResult so i didn't touch it. You can look at the third section in here for a short information.

huangapple
  • 本文由 发表于 2023年6月12日 00:01:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定