spring - 内容类型 'multipart/form-data;boundary=----WebKitFormBoundary...' 不支持 Spring
问题描述
Spring 5.0.7:MVC、数据、安全。我配置multipartResolver
.
我发送下一个 Ajax 请求:
$.ajax({
type: 'POST',
cache: false,
processData: false,
contentType: false,
url: '/api/v1/category/add',
data: new FormData(form)
}).done(result=>{console.log(result);}).fail(result=>{
console.error('ERROR:', result.responseJSON.httpStatus, result.responseJSON.message, result);
self.toast.error('API Error.');
});
但是有一个错误:Content type 'multipart/form-data;boundary=----WebKitFormBoundary6xBCDjCtYYuUVR5c' not supported
为什么?我不明白为什么会发生错误。
控制器:
@RestController
@Secured("hasRole('ADMIN')")
@RequestMapping(value = "/api/v1")
public class ApiController {
private static final Logger LOGGER = LogManager.getLogger(ApiController.class);
@PostMapping(value = "/category/add", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
private Response categoryAdd(Response response, @RequestBody CategoryAddForm categoryAddForm) {
LOGGER.info(categoryAddForm.toString());
return response;
}
}
类别添加表格:
public class CategoryAddForm {
private String name;
private String description;
private MultipartFile preview;
public CategoryAddForm() { }
public CategoryAddForm(String name, String description, MultipartFile preview) {
this.name = name;
this.description = description;
this.preview = preview;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public MultipartFile getPreview() {
return preview;
}
}
我不知道还能写什么,但是 SO 需要更多的文字。(
解决方案
在您的控制器中,使用@RequestParam 而不是@RequestBody。
有同样的问题,它对我有用。有关更多信息,请参阅此SO 答案
推荐阅读
- php - setFetchMode() 似乎没有工作并且没有返回对象?
- javascript - 如何检索JS中括号外的子字符串?
- excel - VBA为什么我的循环卡在第一个项目上,在我确保它不符合标准之后
- iot - Raspberry Pi 3 + Windows IOT Core 在一段时间后崩溃
- testing - 如何清除 Botium 中的测试会话
- windows - 此窗口上按钮的正确顺序是什么(对于 Windows 操作系统标准)?
- python - 使用关系数据库 Flask-SQLAlchemy 查询单表
- c# - 在epplus中转换日期时间格式
- ruby-on-rails - 下拉菜单不选择型号
- discrete-mathematics - SAGE 如何使用 sagemath 在这个等式中找到 e?