title: JPA Entity, JSON serialization
body: <p>I have my JPA Entity class, AssetOrder. And I have defined a custom JSONSerializer for another entity called Product, which is used by AssetOrder entity internally to map the productID from the Product. As we can see the AssetOrder also has a JSONSerializer for formatting the Date objects.
  The Date object JSON serialization works perfectly. However, I am not sure whether the JSONSerializer for the Product works perfectly. When I retrieve the AssetOrder objects from the JPA DAO and try to display on my client page, I get a response saying - Response contains invalid JSON data. I have enclosed the three java classes, AssetOrder.java, Product.java and JSONProductSerializer.java.
  Please help, where I have done wrong.
  Thanks.</p>
  <pre><code>//===================== AssetOrder.java ============================
  
  
  package my.pkg;
  
  import java.util.Date;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  
  import com.fasterxml.jackson.annotation.JsonAutoDetect;
  import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  
  @JsonAutoDetect
  @Entity
  public class AssetOrder {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long orderId;
      private String orderRequesterSignum;
      private String orderOwnerSignum;
  
      @JoinColumn(name = &quot;productID&quot;)
      @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      private Product product;
      
      //private String productId;
      private int duartion;
      private String processInstanceUUID;
      private Date orderDate;
      private Date orderDeliveryDate;
      private Date modifiedDate;
      private String status;
      private String comments;
  
      public Long getOrderId() {
          return orderId;
      }
      public void setOrderId(Long orderId) {
          this.orderId = orderId;
      }
      public String getOrderRequesterSignum() {
          return orderRequesterSignum;
      }
      public void setOrderRequesterSignum(String orderRequesterSignum) {
          this.orderRequesterSignum = orderRequesterSignum;
      }
      public String getOrderOwnerSignum() {
          return orderOwnerSignum;
      }
      public void setOrderOwnerSignum(String orderOwnerSignum) {
          this.orderOwnerSignum = orderOwnerSignum;
      }
      public int getDuartion() {
          return duartion;
      }
      public void setDuartion(int duartion) {
          this.duartion = duartion;
      }
      public String getProcessInstanceUUID() {
          return processInstanceUUID;
      }
      public void setProcessInstanceUUID(String processInstanceUUID) {
          this.processInstanceUUID = processInstanceUUID;
      }
      
      @JsonSerialize(using=JsonDateSerializer.class)
      public Date getOrderDate() {
          return orderDate;
      }
      public void setOrderDate(Date orderDate) {
          this.orderDate = orderDate;
      }
      
      @JsonSerialize(using=JsonDateSerializer.class)
      public Date getOrderDeliveryDate() {
          return orderDeliveryDate;
      }
      public void setOrderDeliveryDate(Date orderDeliveryDate) {
          this.orderDeliveryDate = orderDeliveryDate;
      }
      
      @JsonSerialize(using=JsonDateSerializer.class)
      public Date getModifiedDate() {
          return modifiedDate;
      }
      public void setModifiedDate(Date modifiedDate) {
          this.modifiedDate = modifiedDate;
      }
      
      public String getStatus() {
          return status;
      }
      public void setStatus(String status) {
          this.status = status;
      }
      public String getComments() {
          return comments;
      }
      public void setComments(String comments) {
          this.comments = comments;
      }
      
      @JsonSerialize(using=JsonProductSerializer.class)
      public Product getProduct() {
          return product;
      }
      public void setProduct(Product product) {
          this.product = product;
      }
  }
  
  //============================Product.java=========================
  package my.pkg;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  
  @Entity
  public class Product {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long productID; 
      private String productName;
      private String productCategory;
      private String productDesc; 
      private Long productOwnerId;
      private Long productHierarchyId;
      private String productProcessID;
      private Long productGroupID;
      
      public Long getProductID() {
          return productID;
      }
      public void setProductID(Long productID) {
          this.productID = productID;
      }
      public String getProductName() {
          return productName;
      }
      public void setProductName(String productName) {
          this.productName = productName;
      }
      public String getProductCategory() {
          return productCategory;
      }
      public void setProductCategory(String productCategory) {
          this.productCategory = productCategory;
      }
      public String getProductDesc() {
          return productDesc;
      }
      public void setProductDesc(String productDesc) {
          this.productDesc = productDesc;
      }
      public Long getProductOwnerId() {
          return productOwnerId;
      }
      public void setProductOwnerId(Long productOwnerId) {
          this.productOwnerId = productOwnerId;
      }
      public Long getProductHierarchyId() {
          return productHierarchyId;
      }
      public void setProductHierarchyId(Long productHierarchyId) {
          this.productHierarchyId = productHierarchyId;
      }
      public String getProductProcessID() {
          return productProcessID;
      }
      public void setProductProcessID(String productProcessID) {
          this.productProcessID = productProcessID;
      }
      public Long getProductGroupID() {
          return productGroupID;
      }
      public void setProductGroupID(Long productGroupID) {
          this.productGroupID = productGroupID;
      }
  }
  
  //=================JSONProductSerializer.java=========================
  //the commented part in the below code didn't work either
  //=====================================================================
  package my.pkg;
  
  
  import java.io.IOException;
  
  import org.springframework.stereotype.Component;
  
  import com.fasterxml.jackson.core.JsonGenerator;
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.databind.JsonSerializer;
  import com.fasterxml.jackson.databind.SerializerProvider;
  
  /**
   * Used to serialize com.xxxx.persistence.entity.Product, which is not a common JSON
   * type, so we have to create a custom serialize method;.
   *
   * source: google.com
   */
  @Component
  public class JsonProductSerializer extends JsonSerializer&lt;Product&gt;{
  
      @Override
      public void serialize(Product product, JsonGenerator gen, SerializerProvider provider)
              throws IOException, JsonProcessingException {
  
          synchronized(product) {
              /*gen.writeStartObject();
              gen.writeNumberField(&quot;productID&quot;, product.getProductID());
              gen.writeStringField(&quot;productName&quot;, product.getProductName());
              gen.writeStringField(&quot;productCategory&quot;, product.getProductCategory());
              gen.writeStringField(&quot;productDesc&quot;, product.getProductDesc());
              gen.writeNumberField(&quot;productOwnerId&quot;, product.getProductOwnerId());
              gen.writeNumberField(&quot;productHierarchyId&quot;, product.getProductHierarchyId());
              gen.writeStringField(&quot;productProcessID&quot;, product.getProductProcessID());
              gen.writeNumberField(&quot;productGroupID&quot;, product.getProductGroupID());
              gen.writeEndObject();*/
              gen.writeNumber(product.getProductID());
              gen.writeString(product.getProductName());
              gen.writeString(product.getProductCategory());
              gen.writeString(product.getProductDesc());
              gen.writeNumber(product.getProductOwnerId());
              gen.writeNumber(product.getProductHierarchyId());
              gen.writeString(product.getProductProcessID());
              gen.writeNumber(product.getProductGroupID());
          }
      }
  
  }
  </code></pre>