Reputation: 89
In my browser debug, I can see that there is a date parameter inside my v object (Wed Mar 25 2015 03:00:00 GMT+0300 (Turkey Standard Time)),full text string format.
function saveVehicle(v) {
return $http.post('/shipment/vehicle/save', v).then(function(response) {
return response.data;
})
The problem is in my requestmapping debug, that date parameter comes with null. The server side coding is like this:
@RequestMapping(value="/vehicle/save", method = RequestMethod.POST)
public Vehicle saveVehicle(@RequestBody Vehicle v){
return vehicleRepository.save(v);
}
And my Vehicle model is like this:
@Entity
@Table(name = "VEHICLE", schema = "VV")
public class Vehicle {
@Column(name = "LOADING_DT")
@JsonSerialize(using = TfJsonDateSerializer.class)
@JsonDeserialize(using = TfJsonDateDeSerializer.class)
private Date loadingDate;
Upvotes: 2
Views: 1458
Reputation: 264
It might be that the property into the request body doesn't have the same name of the java Vehicle#loadingDate
attribute.
Assuming your request body has an attribute called loading_date
, you have to map that name to the java attribute as follows:
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonProperty("loading_date")
private Date loadingDate;
Also, it might be a good idea to define a string conversion for dates:
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;
and add getters and setters in case you forgot:
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;
}
Upvotes: 0
Reputation: 21
also try to POST
a well formed object, that ressambles by parameter names your pojo.
v = {
"loading_date": new Date()
}
$http.post(..., v);
Additionally, I see that you're using custom (de)serializers, so please either post their code or be sure that they perform correctly, according to how the JS is serializing the date value
best nas
Upvotes: 0
Reputation: 21
You need to map your object 'v' send from browser into the Java Object 'Vehicle'.
Usually using a json mapper or custom mapping from Map to your Vehicle pojo.
Upvotes: 1