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: "[email protected]"
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

8
  • 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 Commented Oct 23, 2015 at 21:50
  • updated code with console data Commented Oct 23, 2015 at 21:54
  • No, we need the Json passed in the post call to your server Commented Oct 23, 2015 at 21:55
  • i copied from console itself. i added image now Commented Oct 23, 2015 at 21:56
  • 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 Commented Oct 23, 2015 at 22:52

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.