我想構建一個用戶註冊的API,其中我需要用戶基本信息和他/她的個人資料圖片,所以我非常困惑,我該如何實現這個 !我已經做了控制,並要求一個身體,但是當我訪問此API它給了我錯誤的"Unsupported Media Type"
當我設置的內容類型爲multipart/form-data
那麼它給出了一個錯誤: the request was rejected because no multipart boundary was found
Spring Data Rest:如何發送包含請求的多部分文件正文
請幫助我如何發送用戶信息和用戶照片BOTH在修訂SAME請求
:控制器
@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);
}
如果你已經解決了,請分享你的解決方案 –