0

JPA Entity クラスの AssetOrder があります。また、Product という別のエンティティのカスタム JSONSerializer を定義しました。これは、AssetOrder エンティティによって内部で使用され、Product から productID をマップします。ご覧のとおり、AssetOrder には、Date オブジェクトをフォーマットするための JSONSerializer もあります。Date オブジェクトの JSON シリアル化は完全に機能します。ただし、製品の JSONSerializer が完全に機能するかどうかはわかりません。JPA DAO から AssetOrder オブジェクトを取得し、クライアント ページに表示しようとすると、「応答に無効な JSON データが含まれています」という応答が返されます。AssetOrder.java、Product.java、JSONProductSerializer.java の 3 つの Java クラスを含めました。私が間違ったことをしたところを助けてください。ありがとう。

//===================== 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 = "productID")
    @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<Product>{

    @Override
    public void serialize(Product product, JsonGenerator gen, SerializerProvider provider)
            throws IOException, JsonProcessingException {

        synchronized(product) {
            /*gen.writeStartObject();
            gen.writeNumberField("productID", product.getProductID());
            gen.writeStringField("productName", product.getProductName());
            gen.writeStringField("productCategory", product.getProductCategory());
            gen.writeStringField("productDesc", product.getProductDesc());
            gen.writeNumberField("productOwnerId", product.getProductOwnerId());
            gen.writeNumberField("productHierarchyId", product.getProductHierarchyId());
            gen.writeStringField("productProcessID", product.getProductProcessID());
            gen.writeNumberField("productGroupID", 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());
        }
    }

}
4

1 に答える 1

0

実際、問題は com.fasterxml.jackson (jackson の最新バージョン) パッケージを使用した java.util.Date シリアライゼーションにあることが判明しました。org.codehaus.jackson に戻しましたが、すべてが魅力的に機能します。

于 2012-08-29T10:21:17.333 に答える