'Spring Data Rest : how to send Multi-part file with request Body
I want to construct an API of user signup in which I need user basic information and his/her profile picture also so I am very confuse that how can I achieve this ! I have made a controller and requested for a body But When I access this API it gives me error of "Unsupported Media Type" and when I set content Type as multipart/form-data then it gives an error of :
the request was rejected because no multipart boundary was found
Please help How can I send user information and user photo BOTH in SAME request
UPDATED : Controller
@RequestMapping(method = RequestMethod.POST, value = "createRider")
public @ResponseBody ResponseEntity<?> createRider(
@RequestBody CreateRider createRider,Authentication authentication,
PersistentEntityResourceAssembler assembler,@RequestPart(value = "profilePic", required = false) MultipartFile file) {
if (authentication != null && authentication.getPrincipal() != null) {
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
boolean authorized = authorities.contains(new SimpleGrantedAuthority("rights"));
if (authorized==true)
userService.createNewRider(createRider);
else
return ResponseEntity.status(HttpStatus.SC_CONFLICT).body("Logged In user is not admin");
} else {
// Access denied
throw new AccessDeniedException("Not logged in");
}
return ResponseEntity.ok("Rider Created");
}
createRider.java
public class CreateRider {
private String email;
private String name;
private String password;
private String contactNumber;
private String cnicNumber;
private String drivingLicense;
private String reference;
private MultipartFile file;
..getters nad setters
}
userService.createNewRider
public void createNewRider(CreateRider createRider) {
Group group=groupRepo.findOne(Constants.RIDER_USER_GROUP);
User user=new User();
user.setGroup(group);
user.setEmail(createRider.getEmail());
user.setName(createRider.getName());
user.setPassword(createRider.getPassword());
user.setContactNumber(createRider.getContactNumber());
user.setCnicNumber(createRider.getCnicNumber());
user.setDrivingLicense(createRider.getDrivingLicense());
user.setReference(createRider.getReference());
userRepo.save(user);
RiderLocation riderLocation=new RiderLocation();
riderLocation.setRider(user);
riderLocationRepo.save(riderLocation);
///User Photo
UserPhoto userPhoto=photoService.createUserPhoto(createRider.getFile(), user.getId());
userPhotoRepo.save(userPhoto);
}
Solution 1:[1]
You can get the profile picture as a MultipartFile by adding the following to the controller
@RequestPart(value = "profilePic", required = false) MultipartFile profilePic
So the controller method becomes
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
@RequestMapping(method = RequestMethod.POST, value = "createRider")
public @ResponseBody ResponseEntity<?> createRider(
@RequestPart CreateRider createRider,Authentication authentication,
PersistentEntityResourceAssembler assembler, @RequestPart(value = "profilePic", required = false) MultipartFile profilePic) {
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
