0

I am trying to do simple edit for my web application for that i am getting stringify data from frontend, so i converted it into object but then i am getting id as Long value.my code works fine with add new data but whenever i tried to edit it gives error java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String. Please check the screenshot i provided.

Controller.java

    @RequestMapping(value = "/product", method = RequestMethod.POST)
public String managePostProduct(@RequestParam(name = "data", required = false) String add,
        @RequestParam(name = "file") MultipartFile file,
        HttpServletRequest request) {
    try {
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(add);
        Addproductdetails detail = new Addproductdetails();
        OurProducts mProduct = detail.addproduct(obj, file);

        if (mProduct.getId() == 0) {

            productDAO.addOurProducts(mProduct);

        } else {
            productDAO.update(mProduct);
        }

        if (!mProduct.getFile().getOriginalFilename().equals("")) {
            FileUploadUtility.uploadFile(request, mProduct.getFile(), mProduct.getCode());
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return "redirect:/manage/product?success=product";
}

Addproductdetails,java

    public OurProducts addproduct(Object obj , MultipartFile file) {
    ObjectMapper oMapper = new ObjectMapper();
    Map<String, String> prod = oMapper.convertValue(obj, HashMap.class);

    System.out.println("prod "+ prod);
    OurProducts product = new OurProducts();

    if (prod.containsKey("id")) {           
        System.out.println("product === " + prod.values());
    }
    
    int id = prod.containsKey("id") && !prod.get("id").isEmpty() ? Integer.parseInt((String) prod.get("id")) : 0;
    product.setId(id);
    product.setProduct_name(prod.get("product_name"));
    product.setProduct_info(prod.get("product_info"));
    product.setFile(file);
    return product; 
}

breakpoints

enter image description here

please tell me why i am getting id as Long value? How can i fix it?

2
  • Jackson auto convert numerical string. Seems like Prevent automatic String to Integer conversion in Jackson Commented Aug 20, 2020 at 9:07
  • Because the value is numerical in your JSON? If you want more control, map to a properly annotated POJO instead of a HashMap. Possibly mapping to a type reference of a HashMap<String, String> might also work. See here for more info. Commented Aug 20, 2020 at 9:08

1 Answer 1

2

You are getting id field from client with type of Long and trying to put it into an int type. If you can change Product class, I recommend to change its id field type to long so that no casting is needed also you might face data loss in casting long to int.

by the way if you can not change your code or don't want to, here you can fix the problem:

instead of:

int id = prod.containsKey("id") && !prod.get("id").isEmpty() ? Integer.parseInt((String) prod.get("id")) : 0;

use this:

int id = prod.containsKey("id") && !prod.get("id").isEmpty() ? (int) prod.get("id") : 0;

good luck

Sign up to request clarification or add additional context in comments.

5 Comments

the output of prod.get("id") is long, isn't it?
If you check screenshot i am getting key : id and value: Long instead of actual id eg, value: 4
check the updated answer, please and let me know the results
prod.get("id") is long but it prints nothing i got error on that line when i tried to print it.
@Milgo i got java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String when i tried to print id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.