Announcement Announcement Module
Collapse
No announcement yet.
Converting JSON to Object is throwing ClassCastException if byte[] present. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting JSON to Object is throwing ClassCastException if byte[] present.

    I am using Spring Roo to create a web application, but this is not a Spring Roo specific question.

    I have a controller:

    Code:
       @RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")
        public ResponseEntity<String> createFromJson(@RequestBody String json) {
           EmailBean emailBean = EmailBean.fromJsonToEmailBean(json);
        }
    Here is my EmailBean that I am trying to create from the above EmailBean.fromJsonToEmailBean(json) method:
    Code:
    @RooJson
    @RooJavaBean
    @RooToString
    public class EmailBean {
        private String fromAddress;
        private String[] toAddresses;
        private String[] ccAddresses;
        private String[] bccAddresses;
        private String subject;
        private String body;
        private ArrayList<AttachmentBean> attachmentBeans;
    }
    Here is my AttachmentBean:
    Code:
    @RooJson
    @RooJavaBean
    @RooToString
    public class AttachmentBean {
       private String fileName;
       private byte[] content;
    }
    My client code looks something as follows:
    Code:
    //Create new EmailBean object
      EmailBean emailBean = new EmailBean();
      emailBean.setFromAddress("xxx");
      emailBean.setToAddresses(new String[]{"xxx"});
      emailBean.setCcAddresses(new String[]{"xxx"});
      emailBean.setBccAddresses(new String[]{"xxx"});
      emailBean.setSubject("xxx");
      emailBean.setBody("xxx");
    	
      //Create AttachmentBean's
      ArrayList<AttachmentBean> attachmentBeans = new ArrayList<AttachmentBean>();
      AttachmentBean attachmentBean = new AttachmentBean();
      attachmentBean.setFileName("TestAttachment1.pdf");
      InputStream attachmentStream = this.getClass().getClassLoader().getResourceAsStream("attachments/TestAttachment1.pdf");
      byte[] attachmentContent = FileCopyUtils.copyToByteArray(attachmentStream);
      attachmentBean.setContent(attachmentContent);
      attachmentBeans.add(attachmentBean);
      emailBean.setAttachmentBeans(attachmentBeans);
    		
      //Make the request to the server
      HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
    When executing the client code above, the server throws an exception on the controller method at line:
    Code:
    EmailBean emailBean = EmailBean.fromJsonToEmailBean(json);
    Here is the EmailBean.fromJsonToEmailBean method code, which Roo weaves in as compile time so you cant see it in my EmailBean:
    Code:
    public static EmailBean fromJsonToEmailBean(String json) {
       return new JSONDeserializer<EmailBean>().use(null, EmailBean.class).deserialize(json);
    }
    This is the exception thrown:
    Code:
    java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List
    	at flexjson.factories.ArrayObjectFactory.instantiate(ArrayObjectFactory.java:14)
    	at flexjson.ObjectBinder.bind(ObjectBinder.java:86)
    	at flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:139)
    	at flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:17)
    	at flexjson.ObjectBinder.bind(ObjectBinder.java:86)
    	at flexjson.ObjectBinder.bindIntoCollection(ObjectBinder.java:101)
    	at flexjson.factories.ListObjectFactory.instantiate(ListObjectFactory.java:13)
    	at flexjson.ObjectBinder.bind(ObjectBinder.java:86)
    	at flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:139)
    	at flexjson.factories.ClassLocatorObjectFactory.instantiate(ClassLocatorObjectFactory.java:38)
    	at flexjson.ObjectBinder.bind(ObjectBinder.java:86)
    	at flexjson.ObjectBinder.bind(ObjectBinder.java:65)
    	at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:158)
    	at emailengine.service.bean.EmailBean_Roo_Json.ajc$interMethod$emailengine_service_bean_EmailBean_Roo_Json$emailengine_service_bean_EmailBean$fromJsonToEmailBean(EmailBean_Roo_Json.aj:24)
    	at emailengine.service.bean.EmailBean.fromJsonToEmailBean(EmailBean.java:1)
    	at emailengine.service.bean.EmailBean_Roo_Json.ajc$interMethodDispatch1$emailengine_service_bean_EmailBean_Roo_Json$emailengine_service_bean_EmailBean$fromJsonToEmailBean(EmailBean_Roo_Json.aj)
    	at emailengine.web.EmailController.createFromJson(EmailController.java:56)
    Now, it works if I dont set the byte[] on my attachment bean with line:
    Code:
    attachmentBean.setContent(attachmentContent);
    But as soon as there is a byte[] set, I get that exception. It seems to be a problem with deserializing the byte[] (binary).

    Anyone have any idea what the problem may be?
    Last edited by dleerob; Jul 3rd, 2013, 08:44 AM. Reason: Added code snippet.

  • #2
    Seeing as the problem was with the byte[] on the AttachmentBean, I changed it to a String and encoded the byte[] I wanted to transfer into a Base64 encoded String, set it on the AttachmentBean, then on the server side I decoded it again to a byte[]. That worked. However, im still interested in why I was getting that exception when using a byte[] on my AttachmentBean, so if anyone has any ideas it would be appreciated.

    Comment

    Working...
    X