JSON object imparting partial NULL values when pas

2019-09-02 18:57发布

问题:

I have an AJAX call in my javascript as -

var obj = { 
CAEVCode:"TEST2",
CAEVName:"TriggerRedemption",
DateofReceipt:"YAHOO",
LogReference:"test1"
}

var obj1=JSON.stringify(obj);
$.ajax({
    url:'./webapi/input/post',
    type: 'POST',
    data:obj1,
    contentType : "application/json",
    dataType:'json',
    success:function(data)
    {
        var values = JSON.stringify(data);
        alert(values);
    },
    error:function(xhr,textstatus,errorthrown){
        alert(xhr.responseText);
        alert(textstatus);
        alert(errorthrown);
    }
},'json');

When the JavaScript gets invoked and the AJAX call is made, my object gets sent across (POST) to the following URL -> ./webapi/input/post using RESTFul Web Services, the corresponding InputResponse Class that I have is as follows -

@Path("/input")
public class InputResponse {
    SimpleDateFormat dt = new SimpleDateFormat("dd-MM-yyyy");
    InputService inputservice = new InputService();
    @POST
    @Path("/post")
    @Consumes(MediaType.APPLICATION_JSON) 
    @Produces(MediaType.APPLICATION_JSON)
    public Input postInputRecord(Input obj) throws SQLException, ParseException{
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        System.out.println(gson.toJson(obj));

        String CAEVCode=obj.getCAEVCode();
        String CAEVName=obj.getCAEVName();
        String DateOfReceipt =obj.getDateofReceipt();
        String LogReference=obj.getLogReference();
        addUser(new Input(LogReference,CAEVCode,CAEVName,DateOfReceipt));

    return obj;
    }


    public Input addUser(Input input) throws SQLException{
        return inputservice.addUsers(input);
    }

Next up, My POJO is as follows -

    @XmlRootElement
public class Input {       
    private String CAEVCode;
    private String CAEVName;
    private String DateofReceipt;
    private String LogReference;

        public Input() { } 
        public Input(String logReference, String cAEVCode, String cAEVName,
                String dateofReceipt ) {
            super();
            LogReference = logReference;
            CAEVCode = cAEVCode;
            CAEVName = cAEVName;
            DateofReceipt = dateofReceipt;
        }
public String getLogReference() {
            return LogReference;
        }public void setLogReference(String logReference) {
            LogReference = logReference;
        }public String getCAEVCode() {
            return CAEVCode;
        }public void setCAEVCode(String cAEVCode) {
            CAEVCode = cAEVCode;
        }public String getCAEVName() {
            return CAEVName;
        }public void setCAEVName(String cAEVName) {
            CAEVName = cAEVName;
        }public String getDateofReceipt() {
            return DateofReceipt;
        }public void setDateofReceipt(String dateofReceipt) {
            DateofReceipt = dateofReceipt;
        }
}

The issue is that, when I run my page, the JavasScript object is sent successfully with the help of the AJAX call to the InputResponse class but not all the values comes over. 2 Values come null, and the other's come proper. I have tried debugging it all morning but no help. I also have another piece of code for a similar business logic that happens to be working just fine. I have also happened to try out other forms of AJAX calls to get along with the same, but I am still in vain. Any help much appreciated.

回答1:

I'm guessing it has to do with the naming convention of the properties. You need to be careful with abbreviations when depending on the default deserialization. You can override the default behavior by specifying annotations with the desired name on the property. For example, if you are using Jackson, you can add JsonProperty("..")

@JsonProperty("LogReference")
public String getLogReference() { return LogReference; }
@JsonProperty("CAEVCode")
public String getCAEVCode() { return CAEVCode; }
@JsonProperty("CAEVName")
public String getCAEVName() { return CAEVName; }
@JsonProperty("DateofReceipt")
public String getDateofReceipt() { return DateofReceipt; }

You could also use @XmlElement(name = "") instead of @JsonProperty if you have JAXB annotation suport.