Getting following error "Failed to convert from type [java.lang.String] to type [byte] for value" while fetching record from mongoDB

1.4k views Asked by At

I am facing a problem while fetching records from MongoDB. I am researching on StackOverflow for two days but could not find any solution for this error. In MongoDB I use to store email attachment byte array while fetching the document getting converstionFailedException this does not happen for all the documents but some of the documents. I tried to change the datatype from byte[] to string it worked but in response getting string as an output. My output should be byte[] as the file will be downloaded from the front-end.

    package com.maxxton.inbox.email;

import java.io.Serializable;


public class Attachment implements Serializable {

  private static final long serialVersionUID = 1L;

  private Integer attachmentId;

  private String attachmentName;

  private String attachmentType;

  private AccessType accessType;

  private byte[] content;

  public Integer getAttachmentId() {
    return attachmentId;
  }

  public void setAttachmentId(Integer attachmentId) {
    this.attachmentId = attachmentId;
  }

  public byte[] getContent() {
    return content;
  }

  public void setContent(byte[] content) {
    this.content = content;
  }

  public String getAttachmentName() {
    return attachmentName;
  }

  public void setAttachmentName(String attachmentName) {
    this.attachmentName = attachmentName;
  }

  public String getAttachmentType() {
    return attachmentType;
  }

  public void setAttachmentType(String attachmentType) {
    this.attachmentType = attachmentType;
  }

  public AccessType getAccessType() {
    return accessType;
  }

  public void setAccessType(AccessType accessType) {
    this.accessType = accessType;
  }

}

MongoTemplate code to fetch all the messages

    @Override
  public Page<Email> getAllMessagesWithDetail(Pageable pageable, MessageFilter messageFilter) {
    // Criteria finalCriteria = handleCriteria(messageFilter);
    Query query = new Query();
    Query queryWithPaging = query.with(pageable);
    applyMessagesSortBy(query, messageFilter);
    List<Email> messages = Objects.requireNonNull(mongoTemplate.find(queryWithPaging, Email.class).collectList().block());
    Long count = Objects.requireNonNull(mongoTemplate.count(query, Email.class).block());
    return new PageImpl<>(messages, pageable, count);
  }

Error which I received

{
"uniqueId": "c590d728-a8d8-4fe9-b0a7-c3904877f301",
"timestamp": "2022-01-25T06:04:52.963195400Z",
"errorCode": "GEN-900",
"message": "Failed to convert from type [java.lang.String] to type [byte] for value 'iVBORw0KGgoAAAANSUhEUgAAAu4AAAU2CAIAAABFtaRRAA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kTIgLT1kXAREQAREQARGQlFEdEAEREAEREAERCDEBSZkQF56yLgIiIAIiIAIiICmjOiACIiACIiACIhBiApIyIS48ZV0EREAEREAEREBSRnVABERABERABETATWD58uWDBg369ddfv/76608//fTjjz/+yIcf0RI5SZAQyZGoOx9pHOdNykydOrWn48dhGrlVkEwIjB07tkuXLj/++GO9evVeeeUV/uLGB/9MotM1IiACIiACUScwb96877//3gfpUkqUJErSZaWbNynjkDEbnWXNusKnILB06dKOHTs+/fTT5cuX32mnnXbfffddd911xx133HrrrfmLGx/8OUuYDh06ED5FbDolAiIgAiIQHwJ0jRgdU79+/WHDhqEtli1b5p/5RE4Sw4cPJzmUDkmXtW9GUsa/0slPzNSJzz77bM8990SvlCtXbovSfoQhJOG5ytfKmh8cStUfAtOnT+/Vq1cGL0/O7CxZsoT3mHbt2mUZjzNOuUUg5gRmzZqVPQEGepAUCItFixZlH1v6MZCcUTNkIP2rCBkaKXPWWWfts88+7du395rHoAmnjjjiiBEjRpizDKCYFhx/+zv55JMrVar0888/e2MwkVerVs17yvh88sknxPPggw8mC2D8SZdg5GTatGnJQh555JGEQX7aAClMs2HSdHz55Zd/.....

Below screenshot on how attachment content is stored in mongoDB attachment content stored in mongoDB

Please let me know if anything else is required. Thanks in advance to all who have viewed this Question.

0

There are 0 answers