【问题标题】:Can not deserialize value of type int from String无法从 String 反序列化 int 类型的值
【发布时间】:2017-10-19 16:34:44
【问题描述】:

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

"无法读取文档:无法反序列化 int 类型的值 字符串“${product.id}”:不是有效的整数值↵ [来源: java.io.PushbackInputStream@40d1a098;行:1,列:14](通过 参考链:haughton.dvdstore.model.AddToCartPojo["productId"]); 嵌套异常是 com.fasterxml.jackson.databind.exc.InvalidFormatException:不能 从字符串“${product.id}”反序列化 int 类型的值:无效 [来源: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();
}

AddToCartPojo

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

}

【问题讨论】:

标签: java json ajax spring


【解决方案1】:

我建议更改您的AddToCartPojo,使productId 成为String 而不是int

所以改变这个:

private int productId;

到这里:

private String productId;

你也需要改变你的 getter 'n' setter。

【讨论】:

    猜你喜欢
    • 2019-01-03
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 2023-02-02
    • 2015-05-03
    • 1970-01-01
    • 2019-08-09
    相关资源
    最近更新 更多