需求:后端需要同時接收檔案及物件,或者需要接收多個物件
實作方法:后端使用@RequestPart接收
- 物體類
@Data
public class Child {
private String name;
private Integer age;
private List<Integer> list;
}
- Controller
@Slf4j
@RequestMapping("/postman")
@RestController
public class PostmanParam {
@PostMapping(value = "twoGirlfriendsAndFile")
public String postman4(@RequestPart("file") MultipartFile file,
@RequestPart("child1") Child child1,
@RequestPart("child2")Child child2){
if(file != null){
log.error("接收到檔案");
}
return child1.toString()+child2.toString();
}
}
- Postman設定
選擇body ---- form-data —添加隱藏的content type串列


再看一下檔案是否正確傳入到后端:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/345707.html
標籤:其他
