Before beginning my question, English is not accurate because I am Korean.
I use Spring Boot 1.5.14.
I am implementing file upload using FormData and 400 error occured.
1. Javascript
var formData = new FormData();
formData.append('autoSelect', 'autoSelect');
formData.append('file', fileObj);
$.ajax({
url: '/api/portfolios/' + pofolNo + '/main-image',
type: 'PUT',
enctype: 'multipart/form-data',
processData: false,
contentType: false,
data: formData,
async: false,
});
2. Spring Controller (not worked)
@PutMapping("{pofolNo}/main-image")
public CommonApiResponse changePortfolioMainImage(
@PathVariable("pofolNo") Integer pofolNo,
@RequestParam("autoSelect") String autoSelect,
@RequestParam("mainImage") MultipartFile mainImage) {
log.debug("check : {} / {} / {}", pofolNo, autoSelect, mainImage);
return ok(null);
}
The above code results in a 400 error saying that the autoSelect parameter not present.
So I check HttpServletRequest.getParameter("autoSelect")
like this.
3. Spring Controller (worked)
@PutMapping("{pofolNo}/main-image")
public CommonApiResponse changePortfolioMainImage(
@PathVariable("pofolNo") Integer pofolNo,
HttpServletRequest request,
@RequestParam("mainImage") MultipartFile mainImage) {
log.debug("check : {} / {} / {}", pofolNo, request.getParameter("autoSelect"), mainImage);
return ok(null);
}
The above code results is successful.
What is the difference? I can't understand @RequestParam
is not wokred but worked with HttpServletRequest
.