Frage

I have a REST service that has a PUT method which accepts a JSON object from a Client.

I'm using GSON to marshal the client object to JSON:

Gson gson = new Gson();
String json = gson.toJson(vehicle);

This creates a JSON string of a vehicle:

{
"vehicleid":11,
"year":2012,
"licenseplate":"FST-TRK",
"vin":"wddfvglwregbflwu4",
"color":"Cherry",
"ispurchase":"1",
"purchaseprice":122500,
"purchasedate":"Apr 15, 2014 12:00:00 AM",
"vehiclemodel":
    {"vehiclemodelid":2,
    "vehiclemake":
        {"vehiclemakeid":2
         }
    }
}

So all looks fine there so far.

When the request comes in to the Service tho...

@PUT
@Path("put")
@Consumes("application/json")
public Response updateVehicle(Vehicle vehicle){
    int resultStatus = 0;
    try {
        resultStatus = VehicleBLL.updateVehicle(vehicle);
        return Response.status(200).entity(resultStatus).build();
    } catch (ParseException ex) {
        Logger.getLogger(VehicleService.class.getName()).log(Level.SEVERE, null, ex);
        return Response.status(404).entity(resultStatus).build();
    } catch (SQLException ex) {
        Logger.getLogger(VehicleService.class.getName()).log(Level.SEVERE, null, ex);
        return Response.status(404).entity(resultStatus).build();
    }

}

All of the attributes of the Vehicle class are set EXCEPT for the purchasedate. The vehicle class looks like this, pretty standard POJO with notations for the outbound Service object generation:

package com.company.bo;

import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "vehicle")

public class Vehicle {

    private int vehicleid;
    private int year;
    private String licenseplate;
    private String vin;
    private String color;
    private String ispurchase;
    private int purchaseprice;
    private Date purchasedate;
    private Vehiclemodel vehiclemodel;
    private Vehiclestatus vehiclestatus;

    public Vehicle() {
    }

    public Vehicle(int vehicleid) {
        this.vehicleid = vehicleid;
    }


    @XmlElement(required = true)
    public int getVehicleid() {
        return vehicleid;
    }

    public void setVehicleid(int vehicleid) {
        this.vehicleid = vehicleid;
    }

    @XmlElement(required = true)
    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @XmlElement(required = true)
    public String getLicenseplate() {
        return licenseplate;
    }

    public void setLicenseplate(String licenseplate) {
        this.licenseplate = licenseplate;
    }

    @XmlElement(required = true)
    public String getVin() {
        return vin;
    }

    public void setVin(String vin) {
        this.vin = vin;
    }

    @XmlElement(required = true)
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @XmlElement(required = true)
    public String getIspurchase() {
        return ispurchase;
    }

    public void setIspurchase(String ispurchase) {
        this.ispurchase = ispurchase;
    }

    @XmlElement(required = true)
    public int getPurchaseprice() {
        return purchaseprice;
    }

    public void setPurchaseprice(int purchaseprice) {
        this.purchaseprice = purchaseprice;
    }

    @XmlElement(required = true)
    public Date getPurchasedate() {
        return purchasedate;
    }

    public void setPurchasedate(Date purchasedate) {
        this.purchasedate = purchasedate;
    }

    @XmlElement(required = true)
    public Vehiclemodel getVehiclemodel() {
        return vehiclemodel;
    }

    public void setVehiclemodel(Vehiclemodel vehiclemodel) {
        this.vehiclemodel = vehiclemodel;
    }

    @XmlElement(required = true)
    public Vehiclestatus getVehiclestatus() {
        return vehiclestatus;
    }

    public void setVehiclestatus(Vehiclestatus vehiclestatus) {
        this.vehiclestatus = vehiclestatus;
    }

    @Override
    public String toString() {
        return "com.company.bo.Vehicle[ vehicleid=" + vehicleid + " ]";
    }
}

The problem is that when the JSON object hits the REST service and is converted to the Vehicle object, the purchasedate attribute is always null.

I've tried a couple of different GSON Builder formats for the date, but this really appears to be related to the deserialization of the JSON object, I'm just not sure where to head next in trying to resolve this.

War es hilfreich?

Lösung

I'm not sure what was different this time but I tried using the 'T' format with Chrome's Advanced REST Client and that worked ok.

So then I went back and added the same format (which I think I tried previously) to my GSON client conversion to JSON:

Gson gson = new GsonBuilder().setDateFormat("yyy-MM-dd'T'HH:mm:ss").create(); 

And that works AOK now. Not sure what I was missing the first time around but it's working now.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top