java - 无法从 String 反序列化 int 类型的值

标签 java json ajax spring

这是我在控制台中收到的完整错误:

"Could not read document: Can not deserialize value of type int from String "${product.id}": not a valid Integer value↵ at [Source: java.io.PushbackInputStream@40d1a098; line: 1, column: 14] (through reference chain: haughton.dvdstore.model.AddToCartPojo["productId"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type int from String "${product.id}": not a valid Integer value↵ at [Source: java.io.PushbackInputStream@40d1a098; line: 1, column: 14] (through reference chain: 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

@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

//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;

您还需要更改 getter 和 setter。

关于java - 无法从 String 反序列化 int 类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44071858/

相关文章:

java - 未使用 NodeList 获取 XML 标记值

java - 考虑到我的用例,创建自定义数据结构的理想方法

java - 从 Annotation 引用 Triple 的外键具有错误的列数。

jquery - 通过异步 Controller 从浏览器进行 Ajax 调用

java - Java 中的快速哈希表

php - 人类可读的 JSON : aka Add spaces and breaks to json dump

php - 如何使用 PHP 将 JSON 字符串发布到 REST API?

javascript - Datatables 不会重新加载 json 数据

php - 为什么我的 blob 读取为数组?

c# - Ajax 调用不工作