簡體   English   中英

Java類將Ajax JSON帖子接收為null

[英]Java class receives Ajax JSON post as null

我正在嘗試使用ajax post方法將json數據發送到Java類。 當我硬編碼一個json字符串時,我的Java對象就可以了,但是當我嘗試發送使用JSON.stringify()創建的json字符串時,我在控制器中得到了所有null。

這是流程,我在html中有下表:

<table class="table" id="internalUnsubmittedPartRequests">
    <thead>
        <tr>
            <th id="partID">partID</th>
            <th id="quantity">quantity</th>
            <th id="applicableModel">applicableModel</th>
            <th id="queueName">queueName</th>
            <th id="issueType">issueType</th>
            <th id="controlCode">controlCode</th>
            <th id="position">position</th>
            <th id="command">command</th>
            <th id="activationDate">activationDate</th>
            <th id="flushDate">flushDate</th>
        </tr>
    </thead>
    <tbody>
        <!-- Dynamically Generated via newRequest.js -->
    </tbody>
</table>

以及以下從該表讀取的javascript,並使用ajax將其發送到我的控制器:

$('#submit_set_btn').click(function(event) {
    var myRows = [];
    var $headers = $("th");
    var $rows = $("#internalUnsubmittedPartRequests tr").each(function(index) {
      $cells = $(this).find("td");
      myRows[index] = {};
      $cells.each(function(cellIndex) {
        myRows[index][$($headers[cellIndex]).html()] = $(this).html();
      });    
    });
    myRows.shift();
    var myObj = {};
    myObj.myrows = myRows;
    console.log(JSON.stringify(myObj));

    $.ajax({
        type : "POST",
        // contentType : "application/json; charset=utf-8",
        url : window.location,
        data : JSON.stringify(myObj),
        dataType : 'json',
        success : function(result) {
            //alert("success");
            console.log("success");
            console.log(result);
        },
        error : function(e) {
            //alert("fail");
            console.log("fail");
            console.log(e);
        }
    });
});

在console.log(JSON.stringify(myObj))調用期間,將以下內容打印到控制台:

{"myrows":[{"partID":"data","quantity":"data","applicableModel":"data","queueName":"data","issueType":"data","controlCode":"data","position":"data","command":"data","activationDate":"data","flushDate":"data"}]}

這是代表我要創建的對象的Java類:

public class NewPartRequest
{
private String partID;
private String quantity;
private String applicableModel;
private String queueName;
private String issueType;
private String controlCode;
private String position;
private String command;
private String activationDate;
private String flushDate;

@Override
public String toString() {
    return getPartID() + " " + getQuantity() + " " + getApplicableModel() + " " + 
           getQueueName() + " " + getIssueType() + " " + getCommand() + " " + 
           getActivationDate() + " " + getFlushDate();
}


//Getters and Setters below this line

最后,控制器類接收JSON數據並使用toString方法顯示我們得到的結果:

@Controller
public class DistributionNewRequestController {

@RequestMapping(value = "/distribution/internal/unsubmitted/new-request", method = { RequestMethod.GET })
public String newRequest() {
    return "new-request";
}

@RequestMapping(value = "/distribution/internal/unsubmitted/new-request", method = {
        RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public @ResponseBody String internalRequestPost(@RequestBody NewPartRequest newPartRequest) {

    System.out.println("The new part request toString method: ");
    System.out.println(newPartRequest.toString());

    String response = jsonify(newPartRequest);
    return response;

}

private String jsonify(NewPartRequest in) {
    Gson gson = new Gson();
    String out = gson.toJson(in);
    return out;
}

}

在我們的NewPartRequest newPartRequest對象上調用toString()方法,並打印以下內容:

The new part request toString method: 
null null null null null null null null

我已經在這個問題上困擾了很長時間,我無法弄清楚為什么我所有屬性的值都為null。

您需要更改ajax帖子,如下所示

data : JSON.stringify(myObj.myrows[0]),

或者您可以像這樣更改后端代碼

List<NewPartRequest>

然后從如下所示的ajax傳遞

data : JSON.stringify(myObj.myrows),

您實際上是在傳遞對象列表,並且期望在后端控制器方法中使用單個對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM