Open uniquejava opened 6 years ago
http://dolszewski.com/spring/how-to-bind-requestparam-to-object/
最终从这里得到了答案 https://stackoverflow.com/questions/29370143/spring-mvc-upload-file-with-other-fields 前端 XxxEdit.Vue
const data = await form.validate()
try {
let formData = new FormData()
for (let prop in data) {
formData.append(prop, data[prop])
}
fileList.value.forEach((file) => formData.append('files', file))
createKnowledge(formData);
...
}
export function createKnowledge(knowledge: FormData) {
return http.post<void>('/api/knowledges', knowledge, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
}
XxxController.java
@PostMapping
public void create(@Valid KnowledgeInput record) {
knowledgeService.create(record);
}
XxxInput.java
@Data
@ToString
public class KnowledgeInput {
/* other fields */
private String keyword;
/**
* attachments
*/
private MultipartFile[] files;
}
各种方式
@PathVariable
处理uri中的参数@GetMapping("/users/{username}")
https://course.tianmaying.com/spring-mvc+path-variable#
@RequestHeader
、@CookieValue
处理请求头和其中的Cookie部分@RequestParam
即可以处理queryString也可以处理body中的参数, 只能处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST@RequestyBody
处理Content-Type: application/json, application/xml,可能直接映射成对象.@RequestyBody
也可以处理application/x-www-form-urlencoded编码的内容,例如, 但是整个body是一个string类型的字段 -- &拼接的字符串)注意
MultipartFile.transferTo
有严重的bug, 不要用这个方法.另见: https://stackoverflow.com/a/19469782/2497876 另见: https://segmentfault.com/a/1190000014766457