我可以从AJAX post请求传递值而不为该对象创建新类,我需要更新特定字段

时间:2018-01-02 12:27:56

标签: ajax spring spring-boot

此部分用于发出请求 **

我想发送一个数组和另外两个可变数据 我知道我可以使用@PathVarible annotaion但我想要保护,所以有可能不使用它 **

if(arr.length>=1){
           console.log(arr);
           var stored_status = $("#statusselsect").val();
           var selected_status = book_obj.gadBookingStatusMaster.bookingstatusid;
           var token = $("meta[name='_csrf']").attr("content");
           var header = $("meta[name='_csrf_header']").attr("content");
            $(document).ajaxSend(function(e, xhr, options) {
                xhr.setRequestHeader(header, token);
            });
            console.log(book_obj.bookingid);
           if(stored_status == selected_status ){
               alert("Status Must be changed ");
           }else{

               var savedata = {
                     'rooms':arr, 
                    'bookingId':book_obj.bookingid,
                    'status':selected_status 
               }
               console.log(JSON.stringify(savedata));
               $.ajax({
                    type: "POST",
                    contentType: "application/json",
                    url: "<%=path %>/gadbook/bookid/updatebooking/",
                    data: JSON.stringify(savedata),
                    dataType: 'json',
                    cache: false,

                    success: function (data) {
                         event.preventDefault();
                        if(data.msgType==true){

                            swal("Successfull!", data.message, "success");

                             $("#submit").prop("disabled", false);


                        }else if(data.msgType==false) {
                            swal("Error!", data.message, "error");

                        }


                    }
                });

           }

我的Java代码但不正确我在这里失败

@RequestMapping(value={"/gadbook/bookid/updatebooking/"},method = RequestMethod.POST)
          public AjaxDataTransfer updateBooking(Model model,@RequestParam("rooms")  Array rooms,@RequestBody long bookingId,@RequestBody long status){


         System.out.println(bookingId);
         return null;
     }

我不想创建一个类,我想使用提供的数据并稍后获取数据

1 个答案:

答案 0 :(得分:0)

您应该创建一个与您正在发送的有效负载匹配的类,并将其用作请求对象。像这样的东西

@RequestMapping(value={"/gadbook/bookid/updatebooking/"},method = RequestMethod.POST)
  public AjaxDataTransfer updateBooking(@RequestBody Reservation reservation){
     System.out.println(bookingId);
     return null;
  }

模型类

  class Reservation {

    int bookingId;
    String status;
    List<String> rooms


    //Getters, Setters
  }