I'm sending the entity alongside the files on my request as follows (see this answer for more info)
We can send the Entity serialized as JSON in a Blob inside our FormData using the correct Content-Type 'application/json'. With this we can include nested objects, add fields to our Entity -and we won't have to change the Resource Controller method signature with every new field-
curso-update.component.ts
private onBeforeSend(event) {
const token = this.localStorage.retrieve('authenticationToken') || this.sessionStorage.retrieve('authenticationToken');
if (!!token) {
event.xhr.setRequestHeader('Authorization', `Bearer ${token}`);
}
event.formData.append('curso', new Blob([JSON.stringify(this.curso)], { type: 'application/json' }));
}
change the Resource Controller accordingly
CursoResource.java
@PostMapping("/cursos/archivos")
@Timed
public ResponseEntity<Curso> createCurso(@Valid @RequestPart(value = "curso") Curso curso,
@RequestPart("archivos[]")MultipartFile[] archivos) {
...
Curso result = this.cursoService.save(curso);
...
}