Доброго времени суток, не могу понять почему ошибка 415, визуально вроде праильно. Помогите найти ошибку.
Код клиента:
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [photo_url, setPhoto_url] = useState(undefined);
const [width, setWidth] = useState('');
const [compoud, setCompoud] = useState('');
const [density, setDensity] = useState('');
const [video_url, setVideo_url] = useState(undefined);
const [category_id, setCategory_id] = useState('');
const [money, setMoney] = useState('');
const [confirm, setConfirm] = useState();
const addPost = async (e) => {
e.preventDefault();
const myHeaders = new Headers();
myHeaders.append('Max-Header-Size', '16384');
myHeaders.append('Content-Type', 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
const formData = new FormData();
formData.append("title", title);
formData.append('description', description);
formData.append('photo_url', photo_url);
formData.append('width', width);
formData.append('compoud', compoud);
formData.append('density', density);
formData.append('video_url', video_url);
const id = +parseInt(category_id, 10)
formData.append('id', id);
formData.append('money', money);
const response = await fetch(`${URL}${PRODUCTS}${ADD}`, {
method: 'POST',
body: formData,
headers: myHeaders
})
}
Controller :
@PostMapping(value = "/new", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> addProduct (@RequestBody Product product, @RequestParam MultipartFile photo, @RequestParam MultipartFile video) throws IOException {
service.addProductToCategory(product, photo, video);
return ResponseEntity.status(HttpStatus.CREATED).body(product);
}
Сервис:
@Transactional
public void addProductToCategory(Product product, MultipartFile photo, MultipartFile video) throws IOException {
Category category = categoryRepository.findById(product.getCategory().getId())
.orElseThrow(() -> new EntityNotFoundException("Category with id " + product.getCategory().getId() + " not found"));
String photoName = UUID.randomUUID().toString()+photo.getOriginalFilename().split("\\.")[1];
String videoName = UUID.randomUUID().toString()+video.getOriginalFilename().split("\\.")[1];
Path dirPhoto = Path.of(uploaDirProducts_Img);
if (Files.exists(dirPhoto)) {
Files.createDirectory(dirPhoto);
} else {
System.out.println("Ok Photo");
}
Path photoPath = dirPhoto.resolve(photoName);
photo.transferTo(photoPath);
Path dirVideo = Path.of(uploaDirProducts_Video);
if (Files.exists(dirVideo)) {
Files.createDirectory(dirVideo);
} else {
System.out.println("Ok Video");
}
Path videoPath = dirVideo.resolve(videoName);
video.transferTo(videoPath);
product.setCategory(category);
product.setTitle(product.getTitle());
product.setDescription(product.getDescription());
product.setPhotoUrl(URL_IMG+photoName);
product.setWidth(product.getWidth());
product.setCompoud(product.getCompoud());
product.setDensity(product.getDensity());
product.setVideoUrl(URL_VIDEO+videoName);
product.setCreatedAt(Instant.now());
product.setCategory(category);
product.setMoney(product.getMoney());
repository.save(product);
}