当前位置: 首页 > 知识库问答 >
问题:

Spring不支持内容类型“multipart/form-data;boundary=----WebKitFormBoundary…”

壤驷乐邦
2023-03-14

Spring 5.0.7:MVC、数据、安全。我配置multipartresolver

$.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.');
});
@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;
    }
}

共有1个答案

关翰
2023-03-14

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

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

 类似资料: