簡體   English   中英

無法從字符串反序列化int類型的值

[英]Can not deserialize value of type int from String

這是我在控制台中得到的完整錯誤:

“無法讀取文檔:無法從字符串“ $ {product.id}”反序列化類型為int的值:在[Source:java.io.PushbackInputStream@40d1a098; line:1,column:14]處不是有效的Integer值↵ (通過參考鏈:haughton.dvdstore.model.AddToCartPojo [“ productId”]);嵌套異常為com.fasterxml.jackson.databind.exc.InvalidFormatException:無法從字符串“ $ {product.id}中反序列化為int類型的值“:不是有效的Integer值↵,位於[來源:java.io.PushbackInputStream@40d1a098;第1行,第14列](通過參考鏈:haughton.dvdstore.model.AddToCartPojo [“ productId”])”

我的HTML

<form  method="post">
<p>Enter quantity you would like to purchase :
<input type="number" id="quantity" name="quantity" step="any" min="1" max="${product.quantityInStock}" value="1"></input>
 </p>
<input type="submit" class="btn btn-primary"  id="addToCart"  name="button" value="Add to cart"/>
<input type="hidden" id="productId" value='${product.id}'/>
</form>

App.js

  $("#addToCart").click(function(event) {

        var data = {}
        data["productId"] = $("#productId").val();
       data["quantity"] = $("#quantity").val();



        $.ajax({
                 type: "POST",
                 contentType: "application/json",
                 url: "http://localhost:8080/addToCart",
                 data: JSON.stringify(data),
                 dataType: 'json',
                 timeout: 600000,
                 success: function (data) {
                    console.log(data);
                     //...
                 },
                 error: function (e) {

                     //...
                 }
        });
     event.preventDefault();

    });

控制者

@Controller
@Scope("session")
public class CartController {
@Autowired
private Cart cart;
@Autowired
ProductService productService;

@RequestMapping(value="/cart", method= RequestMethod.GET)
public String searchResults(Model model) {
model.addAttribute("cartLines",cart.getLines());
model.addAttribute("cartTotalPrice",cart.getTotalPrice());
    return "cart";
}
@ResponseBody
@RequestMapping(value="/addToCart", method= RequestMethod.POST)
public String searchResults(@RequestBody AddToCartPojo addToCartPojo) {
    Product product = productService.findById(((long) addToCartPojo.getProductId()));
    if(product == null){
        //if the productid supplied doesnt belong to a product in our database
        return "failure";
    }
    FlashMessage result = cart.add(product,addToCartPojo.getQuantity());
      return  result.getStatus().toString();
}

添加到購物車

//pojo for sending via ajax
public class AddToCartPojo {
private int productId;
private int quantity;

public int getProductId() {
    return productId;
}

public void setProductId(int productId) {
    this.productId = productId;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int quantity) {
    this.quantity = quantity;
}

}

我建議更改您的AddToCartPojo以便productIdString而不是int

所以改變這個:

private int productId;

對此:

private String productId;

您還需要更改獲取器的n個設置器。

暫無
暫無

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

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