Spring Data Rest:如何使用请求正文发送多部分文件
Posted
技术标签:
【中文标题】Spring Data Rest:如何使用请求正文发送多部分文件【英文标题】:Spring Data Rest : how to send Multi-part file with request Body 【发布时间】:2017-11-24 02:11:31 【问题描述】:我想构建一个用户注册 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】:您可以通过在控制器中添加以下内容以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)
【讨论】:
那么 requestBody createRider 呢?@RequestBody CreateRider createRider
我还想要其他信息名称、密码等
您可以使用@RequestPart 加载,如答案中所述
好的,等我试试
如何从邮递员发送数据?内容类型应该是什么?以上是关于Spring Data Rest:如何使用请求正文发送多部分文件的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Spring Data Rest 公开 resourceId
如何在 Spring REST 控制器中获取原始 JSON 正文?
Spring Rest Controller PUT 方法请求正文验证?
来自 UI 的 Rest Spring 身份验证调用:在请求正文中传递用户名和密码,而不是在 url 参数中,以使用 Spring Security 进行身份验证