Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring Requestmapping中将日期作为对象参数发布_Java_Spring_Kendo Ui_Http Post - Fatal编程技术网

Java 如何在Spring Requestmapping中将日期作为对象参数发布

Java 如何在Spring Requestmapping中将日期作为对象参数发布,java,spring,kendo-ui,http-post,Java,Spring,Kendo Ui,Http Post,在我的浏览器调试中,我可以看到我的v对象(Wed Mar 25 2015 03:00:00 GMT+0300(土耳其标准时间))中有一个日期参数,为全文字符串格式 function saveVehicle(v) { return $http.post('/shipment/vehicle/save', v).then(function(response) { return response.data; }) 问题在于我的requestm

在我的浏览器调试中,我可以看到我的v对象(Wed Mar 25 2015 03:00:00 GMT+0300(土耳其标准时间))中有一个日期参数,为全文字符串格式

 function saveVehicle(v) {
        return $http.post('/shipment/vehicle/save', v).then(function(response) {
            return response.data;
        })
问题在于我的requestmapping调试中,date参数带有null。服务器端编码如下所示:

@RequestMapping(value="/vehicle/save", method = RequestMethod.POST)
public Vehicle saveVehicle(@RequestBody Vehicle v){
    return vehicleRepository.save(v);
}
@Entity
@Table(name = "VEHICLE", schema = "VV")
public class Vehicle {   

    @Column(name = "LOADING_DT")
    @JsonSerialize(using = TfJsonDateSerializer.class)
    @JsonDeserialize(using = TfJsonDateDeSerializer.class)
    private Date loadingDate;
我的车型是这样的:

@RequestMapping(value="/vehicle/save", method = RequestMethod.POST)
public Vehicle saveVehicle(@RequestBody Vehicle v){
    return vehicleRepository.save(v);
}
@Entity
@Table(name = "VEHICLE", schema = "VV")
public class Vehicle {   

    @Column(name = "LOADING_DT")
    @JsonSerialize(using = TfJsonDateSerializer.class)
    @JsonDeserialize(using = TfJsonDateDeSerializer.class)
    private Date loadingDate;

您需要将对象“v”从浏览器发送到Java对象“Vehicle”中


通常使用json映射器或从映射到您的车辆pojo的自定义映射。

还尝试
发布一个格式良好的对象,该对象通过参数命名您的pojo

v = {
    "loading_date": new Date()
}
$http.post(..., v);
此外,我看到您正在使用自定义(反)序列化程序,因此请根据JS序列化日期值的方式发布它们的代码或确保它们正确执行

最好的
nas

请求主体中的属性可能与java
Vehicle\loadingDate
属性的名称不同

假设您的请求主体有一个名为
loading\u date
的属性,您必须将该名称映射到java属性,如下所示:

import com.fasterxml.jackson.annotation.JsonProperty;

@JsonProperty("loading_date")
private Date loadingDate;
此外,最好为日期定义字符串转换:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonFormat;

@JsonProperty("loading_date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "UTC")
private Date loadingDate;
并添加getter和setter,以防您忘记:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonFormat;

@JsonProperty("loading_date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "UTC")
private Date loadingDate;

public Date getLoadingDate() {
    return loadingDate;
}

public void setLoadingDate(Date loadingDate) {
    this.loadingDate = loadingDate;
}