0

I use spring boot, spring data, twitter bootstrap, and use rest. I try to post a form

  <form id="contactInformationForm" class="form-horizontal" role="form" data-fv-framework="bootstrap" data-fv-icon-valid="glyphicon glyphicon-ok" data-fv-icon-invalid="glyphicon glyphicon-remove" data-fv-icon-validating="glyphicon glyphicon-refresh">

        <div class="form-group">
            <label for="lodgerFirstName" class="col-sm-3 control-label">Prénom</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="lodgerFirstName" name="firstName" placeholder="Entrer le prénom">
            </div>
        </div>

        <div class="form-group">
            <label for="lodgerLastName" class="col-sm-3 control-label">Nom</label>
            <div class="col-sm-9">

                <input type="text" class="form-control" id="lodgerLastName" name="lastName" placeholder="Entrer le nom">
            </div>
        </div>
        ...

    $('#lodgerSave').on('click', function (e) {

        jQuery.ajax({
            type: "post",
            url: "http://localhost:8080/lodgers",
            contentType: "application/json",
            data: $('#contactInformationForm').serialize(),
            dataType: 'json',
            success: function (data, status, jqXHR) {

            },
            error: function (jqXHR, status) {

            }
        });
        e.preventDefault();
    });

On the server side

@RequestMapping(value = "/lodgers", method = RequestMethod.POST)
public LodgerInformation createLodger(@RequestBody @Valid final LodgerInformation lodgerDto) {
    return lodgerService.save(lodgerDto);
}

My dto

public class LodgerInformation {

    private long lodgerId;

    private String firstName;

    private String lastName;

    private Date birthdate;

    private long statusId;

    private Date startDate;

    private Date releaseDate;

    private Phone contactPersonelMobile;

    private List<IdentityCardDto> identityCardList;

When i post i get this error message

"{"timestamp":1436293673415,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Unrecognized token 'firstName': was expecting 'null', 'true', 'false' or NaN\n at [Source: java.io.PushbackInputStream@424485fa; line: 1, column: 11]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'firstName': was expecting 'null', 'true', 'false' or NaN\n at [Source: java.io.PushbackInputStream@424485fa; line: 1, column: 11]","path":"/lodgers"}"

firstName field exist on the web part and dto, any idea?

robert trudel
  • 5,283
  • 17
  • 72
  • 124
  • What does the request/POST data look like? Is it valid JSON? – lemieuxster Jul 07 '15 at 18:41
  • $('#contactInformationForm').serialize() return -> "firstName=bob&lastName=bigras&contactPersonelMobile=1234567890&birthdate=17%2F06%2F2015&previousAddressTel=9875311234&brand=&colorpicker-shortlist=%237bd148&year=2000&immatriculation=" – robert trudel Jul 07 '15 at 18:48
  • 3
    Since the error stems from a `JsonParseException`, and jQuery `serialize` does not create JSON but you are setting the data type as such - it may be you need to send JSON. Maybe something along the lines of http://stackoverflow.com/a/1186309/1165635 ? – lemieuxster Jul 07 '15 at 18:58

0 Answers0