Viking Viking - 3 months ago 130
Java Question

Required request body is missing: public org.springframework.web.servlet.ModelAndView

I inquire how to send information from local storage to spring, there was no answer send Array from local storage to spring, so i tried to do in that way and the new problems appeared
As of now JS it looks like :

function sendToJavaBuy() {
cartWhisky = JSON.parse(localStorage.getItem("shoppingCart"));
var newArray = [];
var i = 0;
for(i in cartWhisky){
newArray[i] = cartWhisky[i].name +"_"+ cartWhisky[i].quantity;
i++;
console.log("newArray[i]"+newArray);

}
$.ajax({
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
type: "POST",
data :
JSON.stringify(newArray),
url: 'buySuccessfulWhisky',

success: function (msg) {
window.location.href = "buySuccessfulWhisky";
//console.log(data)
}
});
}


Java:

@RequestMapping(value = "buySuccessfulWhisky", method ={RequestMethod.GET, RequestMethod.POST})
public ModelAndView view(@RequestBody List<String> newArray) {
System.out.println(newArray);
System.out.println();
for(String properties:newArray) {
String[] split = properties.split("_");
String name = split[0];
Integer quantity = Integer.valueOf(split[1]);
System.out.println("name:= "+name);
System.out.println("quantity:= "+quantity);

// whiskyService.changeInfoInDB(name, quantity);
}
List<WhiskyDTO> whiskyDTOs =whiskyService.seeAllWhisky();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("viewAvailableWhisky", whiskyDTOs);
modelAndView.setViewName("whisky");
return modelAndView;
}
}


I see an exception on the UI:
I read forum, but can't understand what can I do with this mistake.


Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing
this as a fallback.


  • Tue Aug 09 12:11:39 EEST 2016 There was an unexpected error (type=Bad Request, status=400). Required request body is missing:
    public org.springframework.web.servlet.ModelAndView

    com.example.controller.buy.SuccessfulBuyWhiskey.view(java.util.List)




enter image description here

maybee I have problems, becouse of this:
I do table to HTML from JS

var cartArray = listCart();
var table ='';
displayShow();

function displayShow() {
table = '';
for(var r in cartArray){
table +='<tr><td width="50"><img src="' + cartArray[r].photo + '" width=35 height=80></td>\
<td width="100">'+cartArray[r].name+'</td>\
<td width="150">'+cartArray[r].describe+'</td>\
<td width="100">\
<table width="80"><td align="right"><button class="minus" data-name="'+cartArray[r].name+'">-</button></td>\
<td align="center"> '+cartArray[r].quantity +'</td>\
<td align="left"><button class="plus" data-name="'+cartArray[r].name+'">+</button></td></tr></table>\
</td>\
+<td width="50" align="center">'+cartArray[r].price+'</td>\
+<td width="80">' + cartArray[r].total + '</td>\
+<td width="50"><button class="deleteItem" data-name="'+cartArray[r].name+'">X</button></td></tr>';
}
$('#trash').html(table);
$("#totalCart").html(totalPriceInCart());
}

Answer

I find answer, problem was in js, new code:

  cartWhisky = JSON.parse(localStorage.getItem("shoppingCart"));
    var newArray = [];
    var i = 0;
    for (i in cartWhisky) {
        newArray[i] = cartWhisky[i].name + "_" + cartWhisky[i].quantity+"_"+cartWhisky[i].photo;
        i++;
    }
    $.ajax({
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json"
        },
        type: "POST",
        url: '/buySuccessfulWhisky',
        data:

        JSON.stringify(newArray),
        success: function (msg) {
            window.location.href = "warehouseWhisky";
Comments