首页 > 解决方案 > 内容类型 '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 需要更多的文字。(

标签: springrestspring-mvc

解决方案


在您的控制器中,使用@RequestParam 而不是@RequestBody。

有同样的问题,它对我有用。有关更多信息,请参阅此SO 答案


推荐阅读