使用pojo获取null @postmapping

时间:2018-04-01 16:35:28

标签: javascript ajax spring

js代码:

function ajaxPost(){

    // PREPARE FORM DATA
    var formData = {
        word: $("#firstname").val(),
        meaning:  $("#URL").val()
    }
    console.log(formData)
    // DO POST
    $.ajax({
        type : "POST",
        contentType : "application/json",
        url : window.location + "api/customer/save",
        data : JSON.stringify(formData),
        dataType : 'json',
        success : function(result) {
            if(result.status == 'present'){
                $("#postResultDiv").html("<p style='background-color:#7FA7B0; color:white; padding:20px 20px 20px 20px'>" + 
                                            "Post Successfully! <br>" +                                              
                                            result.data);
            }
            else if(result.status == 'wrong'){
                $("#postResultDiv").html("<p style='background-color:#7FA7B0; color:white; padding:20px 20px 20px 20px'>" + 
                                            "Post Successfully! <br>" +
                                            "---> Customer's Info: FirstName = " + 
                                            result.data);
            }



            else{
                $("#postResultDiv").html("<strong>Error</strong>"+result.status);
            }
            console.log(result);
        },
        error : function(e) {
            alert("Error!")
            console.log("ERROR: ", e);
        }
    });

    // Reset FormData after Posting
    resetData();

}

function resetData(){
    $("#firstname").val("");
    $("#URL").val("");
}
})

Word class

public class Word {

    private String word;
    private String meaning;

}

RestWebcontroller:

public Response postCustomer(@RequestBody Word customer) throws Exception {
    cust.add(customer);
    System.out.println(customer.getword());
    System.out.println(customer.getMeaning());
}

传递JSON表单数据:

{word: "book", meaning: "read"}

问题: 的System.out.println(customer.getword());打印书         System.out.println(customer.getMeaning()); print null

传递的第二个json值未映射到Word类中的String变量。 在检查浏览器控制台中的值时,我能够查看正确的json格式 请指教

Spring boot starter thymeleaf(使用1.5.7)

0 个答案:

没有答案