Rest API, Spring 5.0.7. I configure multipartResolver.
I send next Ajax request:
$.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.');
});
But there is an error: Content type 'multipart/form-data;boundary=----WebKitFormBoundary6xBCDjCtYYuUVR5c' not supported
why? i don't understand why error happen.
Controller:
@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;
}
}
CategoryAddForm:
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;
}
}
Comment From: wilkinsona
I suspect there’s a problem with your multipart resolver configuration but I can’t tell for sure as you haven’t described how you have configured it. Generally, no configuration should be necessary as Boot will set things up for you as demonstrated in this guide.
I’m going to close this as there’s no indication of a bug in Spring Boot. If you need further help, please ask on Stack Overflow with a minimal, complete, and verifiable example. If it turns out that there is a bug in Spring Boot we can re-open this issue.
Comment From: Tsyklop
@wilkinsona I`m not using Spring Boot. But i not found where possibly create issue for clean Spring or Spring MVC. So I wrote here. I already asked on StackOverflow - There's no one solution to the problem and do not even ask the details. I do not know what to do.
My MVC Config:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.site.controller"})
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
}
@Bean
public DefaultServletHttpRequestHandler createDefaultServletHttpRequestHandler() {
return new DefaultServletHttpRequestHandler();
}
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(100000);
multipartResolver.setDefaultEncoding("UTF-8");
multipartResolver.setMaxUploadSizePerFile(100000);
return multipartResolver;
}
}
Comment From: mohdabraralikhan
415 Unsupported _media_type I have checked for syntax errors, searched forums for the solution but it never works. I tried using request body, request param and request part. I get the same error "content type 'multipart/form-data;boundary=--------------------------- (followed by some random value which keeps changing) ' not supported