【问题标题】:JSON object imparting partial NULL values when passed to POJO thorugh AJAX POST?JSON 对象在通过 AJAX POST 传递给 POJO 时传递部分 NULL 值?
【发布时间】:2015-11-18 10:03:14
【问题描述】:

我的javascript 中有一个AJAX 呼叫 -

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');

JavaScript 被调用并且AJAX 调用被调用时,我的对象使用RESTFul Web Services 被发送到(POST) 到下面的URL -> ./webapi/input/post,对应的InputResponse 我拥有的类是如下 -

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

接下来,我的POJO如下——

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

问题是,当我运行我的页面时,JavasScript 对象在AJAX 调用InputResponse 类的帮助下成功发送,但并非所有值都出现了。 2个值是空的,另一个是正确的。我整个早上都在尝试调试它,但没有帮助。我还有另一段代码用于类似的业务逻辑,它恰好工作得很好。我也碰巧尝试过其他形式的AJAX 调用来与之相处,但我仍然是徒劳的。非常感谢任何帮助。

【问题讨论】:

    标签: java ajax rest pojo restful-url


    【解决方案1】:

    我猜这与属性的命名约定有关。在依赖默认反序列化时,您需要小心缩写。您可以通过在属性上指定具有所需名称的注释来覆盖默认行为。比如你用的是Jackson,可以加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; }
    

    如果您有 JAXB 注释支持,您也可以使用 @XmlElement(name = "") 代替 @JsonProperty

    【讨论】:

      猜你喜欢
      • 2017-03-02
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 2012-12-11
      • 1970-01-01
      • 2015-08-26
      相关资源
      最近更新 更多