请参阅下面的代码,我要在其中发送具有文件和其他json数据的请求,这些请求将在单个java类中:
我的班级是(使用龙目岛):
@Data
public class CustomFileUploadSearch {
private Long selectedId;
private MultipartFile file;
}
@RequestMapping(method = RequestMethod.POST, path = "/rest/GIER/testFileData",
consumes = {"multipart/form-data"}, produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity bulkActivateInactivate(HttpEntity<CustomFileUploadSearch> entity, HttpServletRequest request) {
CustomFileUploadSearch cfuSearch = entity.getBody();
}
我无法使用此方法。
请帮助我,因为它已经用了1天。
谢谢。
@RestController
public class ChassisInventoryRest {
@SuppressWarnings("rawtypes")
@RequestMapping(method = RequestMethod.POST, path = "/rest/GIER/testFileData",
consumes = {"multipart/form-data"}, produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity bulkActivateInactivate(HttpEntity<CustomFileUploadSearch> entity, HttpServletRequest request) {
CustomFileUploadSearch cfuSearch = entity.getBody();
System.out.println("cfuSearch.getSelectedId():"+cfuSearch.getSelectedId());
System.out.println("cfuSearch.getFile():"+cfuSearch.getFile());
return new ResponseEntity<Map<String, Object>>( new HashMap<String, Object>(), HttpStatus.OK);
}
}
答案 0 :(得分:0)
public class CustomFileUploadSearch {
private Long selectedId;
private MultipartFile file;
public Long getSelectedId() {
return selectedId;
}
public void setSelectedId(Long selectedId) {
this.selectedId = selectedId;
}
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity bulkActivateInactivate(CustomFileUploadSearch entity, HttpServletRequest request) {
System.out.println(entity.getSelectedId());
System.out.println(entity.getFile());
}
答案 1 :(得分:0)
您可以在RestController中显式获取MultipartFile作为请求输入参数。
@RestController
public class ChassisInventoryRest {
@PostMapping(path = "/rest/GIER/testFileData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity bulkActivateInactivate(@RequestParam MultipartFile file, @RequestParam Long selectedId) {
//parameterized constructor
CustomFileUploadSearch cfuSearch = new CustomFileUploadSearch(selectedId, file);
return new ResponseEntity<Map<String, Object>>( new HashMap<String, Object>(), HttpStatus.OK);
}
}