java - 通过 AJAX POST 传递给 POJO 时,JSON 对象传递部分 NULL 值?

标签 java ajax rest pojo restful-url

我的 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 调用时,我的对象将通过 (POST) 发送到以下 URL -> ./webapi/input/post 使用 RESTFul Web Services,我拥有的相应 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;
        }
}

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

最佳答案

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

关于java - 通过 AJAX POST 传递给 POJO 时,JSON 对象传递部分 NULL 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33776984/

相关文章:

java - java Logger中的日志计数器

java - Java中的属性是什么?

java - 多线程程序中的意外输出

javascript - 使用 AJAX 从服务器发送和接收信息

REST - 使用单个 POST 创建嵌套资源

language-agnostic - 自定义操作应该使用哪个动词?

java - 将无状态注入(inject) ManagedBean

javascript - php向ajax发送值失败错误

css - Jquery 将内容显示为链接

python - 禁用或限制/o/应用程序(django rest framework,oauth2)