英文:
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);
}
英文:
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);
}
答案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("/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);
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论