Dans une application web qui utilise jquery et twitterboostrap, on utilise l'architecture rest, spring boot, spring data.
Je tente de sauvegarder un formulaire.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
@RequestMapping(value = "/lodgers", method = {RequestMethod.POST, RequestMethod.PUT})
public LodgerInformationDto saveLodger(@RequestBody @Valid final LodgerInformationDto lodgerDto) {
    return lodgerService.save(lodgerDto);
}
Voici une partie de mes dto (J'ai exclu les get/set)

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
 
public class LodgerInformationDto {
    private long lodgerId;
    private String firstName;
    private String lastName;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
    private Date birthdate;
    private long statusId;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
    private Date entryDate;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
    private Date releaseDate;
    private PhoneDto contactPersonelMobile;
    private List<IdentityCardDto> identityCardList;
    private OldAddressDto oldAddress;
    private VehicleDto vehicle;
}
 
public class PhoneDto {
    private long phoneId;
    private String phone;
    private int extension;
}
 
public class IdentityCardDto {
    private long identityCardId;
    private IdentityCardTypeDto identityCardType;
    private String value;
    private Date expiration;
    private boolean lodgerOwn;
}
 
public class OldAddressDto {
    private long addressId;
    private String name;
    private String address;
    private PhoneDto phone;
}
Il faudrait donc que j'envois une structure similaire a
{
lodgerId : 1,
firstName: "Paul",
lastName: "Smith",
phone{
phoneId: 1,
phone: "6753451212"
extension: ""
},
identityCardDtoList: [{
identityCardId: 11,
identityCardTypeDto:{
identityCardTypeId: 111,
identityCardType: "Node 1.1.1",
expiration: 12/12/2015,
hasExpirationDate: true
}
},{
identityCardId: 12,
identityCardTypeDto:{
identityCardTypeId: 112,
identityCardType: "Node 1.1.2",
expiration: 12/12/2015,
hasExpirationDate: true
}
}]
}

Au niveau du web, j'ai

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
 
<form id="contactInformationForm">
<input type="hidden" class="form-control" name="lodgerId" id="lodgerId">
<input type="text" class="form-control" id="lodgerFirstName" name="firstName" >
<input type="text" class="form-control" id="lodgerLastName" name="lastName" >
<input type="text" class="form-control" id="contactPersonelMobile" name="contactPersonelMobile.phone">
<input type="text" class="form-control" id="contactPersonelMobile" name="contactPersonelMobile.phone">
 
<input type="hidden" id="oldAddressAddressId" name="oldAddress.addressId">
<input type="text" class="form-control" id="oldAddressName" name="oldAddress.name">
<input type="text" class="form-control" id="oldAddressAddress" name="oldAddress.address" >
<input type="hidden" id="oldAddressPhoneId" name="oldAddress.phone.phoneId">
<input type="text" class="form-control" id="oldAddressPhone" name="oldAddress.phone.phone">
 
//j'en ai 2 autres comme ça dans la page
<input type="hidden" name="identityCardList[0].identityCardId">
<select id="identityCardType1" name="identityCardList[0].identityCardType" class="form-control"></select>
<input type="text" class="form-control" id="idCardValue1" name="identityCardList[0].value" >
<input type="text" id="expirationDateCard1" name="identityCardList[0].expiration" class="form-control">
<input type="checkbox" name="identityCardList[0].lodgerOwn" value="">
 
var type = "post";
var url = "http://localhost:8080/lodgers";
var frm = $('#contactInformationForm').serializeObject();
var data = JSON.stringify(frm);
 
jQuery.ajax({
	type: type,
	url: url,
	contentType: "application/json",
	data: data,
	dataType: 'json',
	success: function (data, status, jqXHR) {
	},
	error: function (jqXHR, status) {
	}
});
 
$.fn.serializeObject = function ()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
Lorsque j'arrive au serveur, je remarque que tous les objets de mon dto soit: oldAddress, vehicle, identityCardList, contactPersonelMobile sont null alors que les champs tel que firstName, lastName... sont bien remplie

Une idée