Angularjs 如何将引导datetimepicker字符串转换为java datetime

Angularjs 如何将引导datetimepicker字符串转换为java datetime,angularjs,spring,spring-mvc,datetime,Angularjs,Spring,Spring Mvc,Datetime,角度控制器 $http({ method: 'POST', url: '/Eatery/save', contentType:'application/json', dataType:'json', data:resvnCtrl.user

角度控制器

                $http({
                      method: 'POST',
                      url: '/Eatery/save',
                      contentType:'application/json',
                      dataType:'json',
                      data:resvnCtrl.user
                })
@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);   
    }
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);
        }

    }

}
Spring mvc控制器

                $http({
                      method: 'POST',
                      url: '/Eatery/save',
                      contentType:'application/json',
                      dataType:'json',
                      data:resvnCtrl.user
                })
@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);   
    }
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);
        }

    }

}
Java模型

@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;
    }

}
从浏览器控制台

email: "kerhb@regerg.e"
name: "kjergk"
note: "wefwef"
phone: "1234567899"
seats: 2
time: "10/23/2015 5:53 PM"
自定义日期反序列化程序

                $http({
                      method: 'POST',
                      url: '/Eatery/save',
                      contentType:'application/json',
                      dataType:'json',
                      data:resvnCtrl.user
                })
@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);   
    }
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);
        }

    }

}
public类CustomJsonDateDeserializer扩展了JsonDeserializer
{
@凌驾
公共日期反序列化(JsonParser JsonParser,
反序列化上下文(DeserializationContext)引发IOException、JsonProcessingException{
SimpleDateFormat=新的SimpleDateFormat(“yyyy-MM-dd-HH:MM”);
String date=jsonparser.getText();
试一试{
返回格式.parse(日期);
}捕获(解析异常){
抛出新的运行时异常(e);
}
}
}

我在UI上有一个引导datetimepicker,在后端有一个JavaRESTWebService。当我发送日期选择时,我得到“客户端发送的请求在语法上不正确”。发送的datetime字符串未映射到java模型。有人能发现我的错误吗?

@Marged
说你没有在约会模式中涵盖
AM/PM
是正确的。正确的模式应该是
yyyy-MM-dd HH:MM a
。还请注意,您不需要为此定制反序列化程序,而是可以使用


在浏览器中从f12创建网络跟踪,并向我们显示传递的Json。从代码上看,它似乎与保留所需的完全不同,它包含了使用控制台数据更新的代码。不,我们需要在对服务器的post调用中传递Json。我从控制台本身复制了Json。我现在添加了图像好主意。。。你有没有忘记在约会结束时照顾
PM
?如果这不是问题的根源,我的最后一个建议是创建一个端点,它从保留中创建一个JSON,以便与Javascript发送的值进行比较