Bonjour
J'essaie "d'uploader" un fichier via un formulaire dans mon application spring boot, mais je reçois ce message d'erreur:
J'utilisa ajax pour la transmission des données vers le contrôleur.org.springframework.orm.jpa.JpaSystemException: could not serialize; nested exception is org.hibernate.type.SerializationException: could not serialize
voici mon formulaire:
Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 <form:form modelAttribute="ajouter-medecin" method="POST" enctype="multipart/form-data" id="form_ajouter_medecin" > <div class="card-box"> <h3 class="card-title" >Informations personnelles!</h3> <div class="text-center m-t-20"> <span class="resultat text-center"> </span> </div> <div class="row"> <div class="col-md-12"> <div class="profile-img-wrap"> <img class="inline-block" id="blah" src="#" alt=""> <div class="fileupload btn"> <span class="btn-text">Ajouter une photo</span> <input class="upload" type="file" id="imgInp" name="files"> <span class="erreur erreur_image" ></span> </div> </div> <div class="profile-basic"> <div class="row"> <div class="col-md-6"> <div class="form-group form-focus"> <form:label path="matricule" class="focus-label">Matricule:<span class="text-danger">*</span></form:label> <form:input path="matricule" id="matricule" class="form-control floating" /> <span class="erreur erreur_matricule" ></span> </div> </div>
Voici mon fichier ajax:
Code JavaScript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 var form = $('#form_ajouter_medecin')[0]; var formData = new FormData(form); /* Send the data using post and put the results in a div */ request =$.ajax({ type: "post", enctype: 'multipart/form-data', url: "ajouter-medecin", data: formData, processData: false, contentType: false, cache: false, beforeSend: function() { $('#load-img').show(); }, success: function(data, textStatus, jqXHR){ $('#load-img').hide(); console.log("SUCCESS : ", data); var result=data.split("+"); var t = result[0]; console.log(t); if(t==="ok" ) { alert('Médecin ajouté avec succès!'); window.location = 'medecins'; } else { $('.resultat').html(t); $('.resultat').css('color', 'red'); $('html,body').animate({scrollTop: $("#monancre").offset().top}, 'slow' ); } }, error:function(jqXHR, textStatus, errorThrown){ $('#load-img').hide(); console.log("ERROR : ", jqXHR.responseText); $('.erreur_user').html("Impossible de se connecter à la BDD!"); } }); return false; }
Et voici mon contrôleur:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 @RequestMapping(value = "/ajouter-medecin", method = RequestMethod.GET) public String showAjouterMedecinPage(ModelMap model) { model.put("name", getLoggedinUserName()); model.addAttribute("ajouter-medecin", new Medecin()); return "ajouter-medecin"; } @RequestMapping(value = "/ajouter-medecin", method = RequestMethod.POST) public ResponseEntity<?> addMedecin(@ModelAttribute Medecin medecin, BindingResult result) { String rst = null; if (result.hasErrors()) { return new ResponseEntity<>("Error: " , HttpStatus.BAD_REQUEST); } Medecin medecinExist = medecinService.findByMatricule(medecin.getMatricule()); if (medecinExist != null) { return new ResponseEntity<>("Ce Matricule exite déjà!!!: " , HttpStatus.BAD_REQUEST); } else { ////////// try { rst = this.saveUploadedFiles(medecin.getFiles()); } // Here Catch IOException only. // Other Exceptions catch by RestGlobalExceptionHandler class. catch (IOException e) { e.printStackTrace(); return new ResponseEntity<>("Error: " + e.getMessage(), HttpStatus.BAD_REQUEST); } /////////// medecinService.saveMedecin(medecin); return new ResponseEntity<String>("ok" + result, HttpStatus.OK); } } private String saveUploadedFiles(MultipartFile[] files) throws IOException { // Make sure directory exists! File uploadDir = new File(UPLOAD_DIR); uploadDir.mkdirs(); StringBuilder sb = new StringBuilder(); for (MultipartFile file : files) { if (file.isEmpty()) { continue; } String uploadFilePath = UPLOAD_DIR + "/" + file.getOriginalFilename(); byte[] bytes = file.getBytes(); Path path = Paths.get(uploadFilePath); Files.write(path, bytes); sb.append(uploadFilePath).append("<br/>"); } return sb.toString(); }
Je ne sais vraiment pas ce qui cloche.
Merci
Partager