728x90
FE: Nuxt(Vue)
FE에서 등록할 때 등록할 값들과 사진이 함께 전달됨 (multipart/form-data)
(this.$axios.post(
('https://'),
formData,
{
withCredentials: true,
headers: {
'Content-Type': 'multipart/form-data',
Authorization: // token
}
}
)
).then(res => {
}).catch((error) => {
window.alert(error.response.data.message)
})
formData 만드는 법
const formData = new FormData()
formData.append(key, value)
** 사진을 여러장 보낼 때
this.files.map((file) => {
formData.append('image', file)
})
BE: Spring
Controller에서 FormData를 RequestBody로 받을 수 없다. RequestBody는 String만 처리 가능하기 때문
1. RequestPart 사용
2. ModelAttribute 사용
RequestPart
@PostMapping(value = "/test")
public ResponseEntity register(@RequestPart Params params,
@RequestPart MultipartFile images) {
return ResponseEntity.status(HttpStatus.OK).body(1);
}
public class Params {
String name;
Integer price;
}
ModelAttribute
@PostMapping(value = "/test")
public ResponseEntity register(@ModelAttribute Params params) {
return ResponseEntity.status(HttpStatus.OK).body(1);
}
public class Params {
String name;
Integer price;
MultipartFile image;
}
참고
https://minholee93.tistory.com/entry/Spring-Json-with-MultipartFile
728x90
'Spring' 카테고리의 다른 글
[ERROR] Port already in use: 1099 (0) | 2022.11.17 |
---|---|
no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (0) | 2022.10.12 |
BeanDefinitionStoreException: Failed to parse configuration class (0) | 2022.09.14 |
DTO, VO, Entity (0) | 2022.09.07 |
Builder 패턴 (0) | 2022.09.02 |