簡體   English   中英

Java Spring Boot HTTP POST請求不起作用

[英]java spring boot HTTP POST request not working

對於我的應用程序,我正在編寫一個POST請求,以從復選框列表中發送參數數組。 它適用於獲取請求,但不適用於后期請求。 我的代碼中有什么錯誤。

我在客戶端的代碼,用於向服務器發送ajax請求。

$(".add").click(function(){

    monitoring.length=0;
    nonMonitoring.length=0;
    $('.modal-body input:checked').each(function() {
        monitoring.push($(this).val());
        });

    $('.addkeywords input:checked').each(function() {
        nonMonitoring.push($(this).val());
        });


//  alert(monitoring[2]+ " " + nonMonitoring[2]);
    var monitoringLength=monitoring.length;
    var nonMonitoringLength=nonMonitoring.length;

    $.ajax({
            type : "POST",
            url : '/rest/channelstats/my/rest/controller',
            data : {
            //  monitoring : monitoring,
            //  nonMonitoring: nonMonitoring,
                monitoringLength: monitoringLength,
                nonMonitoringLength: nonMonitoringLength,

            },
            success : function(data) {

            //  var keywordsList=data
                //console.log(keywordsList);
            //  htm = "" ;


            }


});


    })

我在服務器端的Java代碼。

@RequestMapping(value="/rest/channelstats/my/rest/controller",method = RequestMethod.POST)
public void monitorKeywords(@RequestParam(value="monitoringLength",required=true)int monitoringLength,@RequestParam(value="nonMonitoringLength",required=true)int nonMonitoringLength){
    System.out.println("MonitoringLength =>" +monitoringLength);
    System.out.println("NonMonitoringLength=>" +nonMonitoringLength);

}

}

它適用於HTTP GET請求,但不適用於POST請求。如何解決此問題?

根據您的jquery發布請求,您應該使用DAO (數據訪問對象)來解析請求數據。 所以你應該添加類Request

public class Request {

    private int monitoringLength;
    private int nonMonitoringLength;

    //getters and setters
} 

並將控制器更改為

@RequestMapping(value="/rest/channelstats/my/rest/controller",method = RequestMethod.POST)
public void monitorKeywords(@RequestBody Request request){
    System.out.println("MonitoringLength =>"+request.getMonitoringLength());            
    System.out.println("NonMonitoringLength=>"+request.getNonMonitoringLength());
}

暫無
暫無

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

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