Spring Data Rest:如何使用请求Body

时间:2017-06-21 09:07:02

标签: java json spring rest multipartform-data

我想构建一个用户注册的API,其中我需要用户基本信息和他/她的个人资料图片,所以我很困惑,我怎么能实现这一点!我已经制作了一个控制器并请求了一个正文但是当我访问这个API时,它给出了"Unsupported Media Type"的错误,当我将内容类型设置为multipart/form-data时,它会给出一个错误: the request was rejected because no multipart boundary was found

请帮助如何在同一请求中发送用户信息和用户照片

更新:控制器

@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);



    }

1 个答案:

答案 0 :(得分:0)

通过将以下内容添加到控制器

,您可以将个人资料图片显示为MultipartFile

@RequestPart(value = "profilePic", required = false) MultipartFile profilePic

因此控制器方法变为

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) {

}