1

enter image description hereangular controller

                $http({
                      method: 'POST',
                      url: '/Eatery/save',
                      contentType:'application/json',
                      dataType:'json',
                      data:resvnCtrl.user
                })

Spring mvc controller

@RequestMapping(value="/save",method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public int save(@RequestBody Reservation reservation) {
        System.out.println(reservation.getTime());
        return reservationRepo.save(reservation);   
    }

Java model

@Entity
@Table(name="reservations")
public class Reservation implements Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String cnf;
    private String name;
    private String email;
    private String phone;

    @JsonDeserialize(using=CustomJsonDateDeserializer.class)
    private LocalDateTime time;
    private int seats;
    private String note;

    public Reservation() { }

    public Reservation(String cnf, String name, String email, String phone,
            LocalDateTime time, int seats, String note) {
        this.cnf = cnf;
        this.name = name;
        this.email = email;
        this.phone = phone;
        this.time = time;
        this.seats = seats;
        this.note = note;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCnf() {
        return cnf;
    }

    public void setCnf(String cnf) {
        this.cnf = cnf;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }


    public LocalDateTime getTime() {
        return time;
    }

    public void setTime(LocalDateTime time) {
        this.time = time;
    }

    public int getSeats() {
        return seats;
    }

    public void setSeats(int seats) {
        this.seats = seats;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

}

From browser console

email: "kerhb@regerg.e"
name: "kjergk"
note: "wefwef"
phone: "1234567899"
seats: 2
time: "10/23/2015 5:53 PM"

Custom date deserializer

public class CustomJsonDateDeserializer extends JsonDeserializer<Date>
{
    @Override
    public Date deserialize(JsonParser jsonparser,
            DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = jsonparser.getText();
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

}

I have a bootstrap datetimepicker on UI and a java REST webservice at the backend. when i send date select, i got "The request sent by the client was syntactically incorrect.". the datetime string which is sent did not map to the java model. can someone spot my error

lch
  • 4,569
  • 13
  • 42
  • 75
  • Create a network trace from f12 in your browser and show us the Json passed. Looking at the code it seems to be completely different from what reservation needs to contain – Marged Oct 23 '15 at 21:50
  • updated code with console data – lch Oct 23 '15 at 21:54
  • No, we need the Json passed in the post call to your server – Marged Oct 23 '15 at 21:55
  • i copied from console itself. i added image now – lch Oct 23 '15 at 21:56
  • Possible duplicate of [Date format Mapping to JSON Jackson](http://stackoverflow.com/questions/12463049/date-format-mapping-to-json-jackson) – Marged Oct 23 '15 at 21:59
  • You need to tell your Json library (assumption: Jackson) how to convert the date. Other choice is to deliver a timestamp. http://stackoverflow.com/questions/12463049/date-format-mapping-to-json-jackson – Marged Oct 23 '15 at 22:00
  • i used custom date deserializer and used that in my model, but still it produced the same error. updated that above – lch Oct 23 '15 at 22:50
  • 1
    Good idea ... Did you perhaps forget to take care of the `PM` at the end of the date ? If this is not the root of the problem my last suggestion is to create an endpoint which creates a JSON from a Reservation for you to compare with the values sent by Javascript – Marged Oct 23 '15 at 22:52

1 Answers1

2

@Marged is rigth saying that you didn't cover AM/PM in your date pattern. The proper patter would be yyyy-MM-dd HH:mm a. Note also that you don't need a custom deserializer for this, can rather use @DateTimeFormat

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm a")
private LocalDateTime time;
Master Slave
  • 27,771
  • 4
  • 57
  • 55