2017-07-26 39 views
0

我在Java中的實體如下:如何獲得內部類的所有屬性作爲Java的外部類的一部分,而轉換成JSON

@Entity 
@Table(name="ENTITY_MASTER") 
@JsonIgnoreProperties(ignoreUnknown=true) 
public class EntityMaster implements Serializable 
{ 
    @Id 
private EntityMasterPK entityMasterPK; 

public EntityMasterPK getEntityMasterPK() { 
    return entityMasterPK; 
} 

public void setEntityMasterPK(EntityMasterPK entityMasterPK) { 
    this.entityMasterPK = entityMasterPK; 
} 

@Column(name="ENTITY_BUSINESS_NAME") 
private String entityBusinessName; 

@Column(name="ENTITY_BUSINESS_NAME_ALT_LANG") 
private String entityBusinessNameAltLang; 

@Column(name="IS_ACTIVE") 
private String isActive; 

@Column(name ="GROUP_NAME") 
private String groupName; 

@Column(name="ENTITY_DESCRIPTION") 
private String entityDescription; 

@Column(name="IS_SURROGATE") 
private String isSurrogate; 

@Column(name="IS_GL_ENTITY") 
private String isgL; 

@Column(name="RECORD_DELETE_ALLOWED") 
private String recordDeleteAllowed; 

@Column(name="IS_RPF_REQUIRED") 
private String isRpfRequired; 

@Column(name="LOCK_REQUIRED") 
private String lockRequired; 

public String getIsRpfRequired() { 
    return isRpfRequired; 
} 

public void setIsRpfRequired(String isRpfRequired) { 
    this.isRpfRequired = isRpfRequired; 
} 

public String getLockRequired() { 
    return lockRequired; 
} 

public void setLockRequired(String lockRequired) { 
    this.lockRequired = lockRequired; 
} 

public String getRecordDeleteAllowed() { 
    return recordDeleteAllowed; 
} 

public void setRecordDeleteAllowed(String recordDeleteAllowed) { 
    this.recordDeleteAllowed = recordDeleteAllowed; 
} 

public String getIsgL() { 
    return isgL; 
} 

public void setIsgL(String isgL) { 
    this.isgL = isgL; 
} 

@Embeddable 
public static class EntityMasterPK implements Serializable 
{ 
    @Column(name="SOLUTION_ID") 
    private Integer solutionId; 

    @Column(name="ENTITY_NAME") 
    private String entityName; 

    public Integer getSolutionId() { 
     return solutionId; 
    } 

    public void setSolutionId(Integer solutionId) { 
     this.solutionId = solutionId; 
    } 

    public String getEntityName() { 
     return entityName; 
    } 

    public void setEntityName(String entityName) { 
     this.entityName = entityName; 
    } 

} 
public String getEntityBusinessName() { 
    return entityBusinessName; 
} 

public void setEntityBusinessName(String entityBusinessName) { 
    this.entityBusinessName = entityBusinessName; 
} 

public String getEntityBusinessNameAltLang() { 
    return entityBusinessNameAltLang; 
} 

public void setEntityBusinessNameAltLang(String entityBusinessNameAltLang) { 
    this.entityBusinessNameAltLang = entityBusinessNameAltLang; 
} 

public String getIsActive() { 
    return isActive; 
} 

public void setIsActive(String isActive) { 
    this.isActive = isActive; 
} 

public String getGroupName(){ 
    return groupName; 
} 

public void setGroupName(String groupName){ 
    this.groupName=groupName; 
} 

public String getIsSurrogate(){ 
    return isSurrogate; 
} 

public void setIsSurrogate(String isSurrogate){ 
    this.isSurrogate=isSurrogate; 
} 

public String getEntityDescription(){ 
    return entityDescription; 
} 

public void setEntityDescription(String entityDescription){ 
    this.entityDescription=entityDescription; 
} 
} 

我想serailize以JSON格式該類發到我的前端 當我將其轉換成JSON我得到的東西,如:{"entityBusinessName":"..","groupName":"abbc","entityMasterPK":{"solutionId":"100","entityName":"abcd"},..}

我怎樣才能得到entityMasterPk的變量也作爲我希望我的JSON來是這個樣子的主要JSON:{"entityBusinessName":"..","groupName":"abbc","solutionId":"100","entityName":"abcd",..}, 我正在使用jpa,如果任何人有解決這個問題,請幫忙。

+0

使用前端所需的所有字段創建數據傳輸對象。手動設置您的對象變量。 –

回答

0

我想你可以通過getter和setter這樣的代碼,但不知道這樣做:

@Entity 
@Table(name="ENTITY_MASTER") 
@JsonIgnoreProperties(ignoreUnknown=true) 
public class EntityMaster implements Serializable 
{ 
    @Id 
private EntityMasterPK entityMasterPK; 

public EntityMasterPK getEntityMasterPK() { 
    return entityMasterPK; 
} 

public void setEntityMasterPK(EntityMasterPK entityMasterPK) { 
    this.entityMasterPK = entityMasterPK; 
} 
@Column(name="ENTITY_BUSINESS_NAME") 
private String entityBusinessName; 

@Column(name="ENTITY_BUSINESS_NAME_ALT_LANG") 
private String entityBusinessNameAltLang; 

@Column(name="IS_ACTIVE") 
private String isActive; 

@Column(name ="GROUP_NAME") 
private String groupName; 

@Column(name="ENTITY_DESCRIPTION") 
private String entityDescription; 

@Column(name="IS_SURROGATE") 
private String isSurrogate; 

@Column(name="IS_GL_ENTITY") 
private String isgL; 

@Column(name="RECORD_DELETE_ALLOWED") 
private String recordDeleteAllowed; 

@Column(name="IS_RPF_REQUIRED") 
private String isRpfRequired; 

@Column(name="LOCK_REQUIRED") 
private String lockRequired; 

//====this code ===== 
@Column(name="SOLUTION_ID") 
private Integer getSolutionId() { 
    return entityMasterPK.getSolutionId(); 
} 

private void setSolutionId(Integer solutionId) { 
    entityMasterPK.setSolutionId(solutionId); 
} 


@Column(name="ENTITY_NAME") 
private String getEntityName() { 
    return entityMasterPK.getEntityName(); 
} 

private void setEntityName(String entityName) { 
    entityMasterPK.setEntityName(entityName); 
} 
//======end code ====== 
public String getIsRpfRequired() { 
    return isRpfRequired; 
} 

public void setIsRpfRequired(String isRpfRequired) { 
    this.isRpfRequired = isRpfRequired; 
} 

public String getLockRequired() { 
    return lockRequired; 
} 

public void setLockRequired(String lockRequired) { 
    this.lockRequired = lockRequired; 
} 

public String getRecordDeleteAllowed() { 
    return recordDeleteAllowed; 
} 

public void setRecordDeleteAllowed(String recordDeleteAllowed) { 
    this.recordDeleteAllowed = recordDeleteAllowed; 
} 

public String getIsgL() { 
    return isgL; 
} 

public void setIsgL(String isgL) { 
    this.isgL = isgL; 
} 

@Embeddable 
public static class EntityMasterPK implements Serializable 
{ 
    @Column(name="SOLUTION_ID") 
    private Integer solutionId; 

    @Column(name="ENTITY_NAME") 
    private String entityName; 

    public Integer getSolutionId() { 
     return solutionId; 
    } 

    public void setSolutionId(Integer solutionId) { 
     this.solutionId = solutionId; 
    } 

    public String getEntityName() { 
     return entityName; 
    } 

    public void setEntityName(String entityName) { 
     this.entityName = entityName; 
    } 

} 
public String getEntityBusinessName() { 
    return entityBusinessName; 
} 

public void setEntityBusinessName(String entityBusinessName) { 
    this.entityBusinessName = entityBusinessName; 
} 

public String getEntityBusinessNameAltLang() { 
    return entityBusinessNameAltLang; 
} 

public void setEntityBusinessNameAltLang(String entityBusinessNameAltLang) { 
    this.entityBusinessNameAltLang = entityBusinessNameAltLang; 
} 

public String getIsActive() { 
    return isActive; 
} 

public void setIsActive(String isActive) { 
    this.isActive = isActive; 
} 

public String getGroupName(){ 
    return groupName; 
} 

public void setGroupName(String groupName){ 
    this.groupName=groupName; 
} 

public String getIsSurrogate(){ 
    return isSurrogate; 
} 

public void setIsSurrogate(String isSurrogate){ 
    this.isSurrogate=isSurrogate; 
} 

public String getEntityDescription(){ 
    return entityDescription; 
} 

public void setEntityDescription(String entityDescription){ 
    this.entityDescription=entityDescription; 
} 
} 
0

我猜你正在使用傑克遜或類似的東西。所以我所做的就是我做了一個演示類

public static class Jack implements Serializable{ 

     private String name; 
     private SonOfJack sonOfJack; 
     public Jack(String name, String id, String topic) { 
      this.name = name; 
      sonOfJack = new SonOfJack(id, topic); 
     } 

     public String getName() { 
      return name; 
     } 

     public SonOfJack getSonOfJack() { 
      return sonOfJack; 
     } 

     public static class SonOfJack implements Serializable{ 
      private String id, topic; 

      public SonOfJack(String id, String topic) { 
       this.id = id; 
       this.topic = topic; 
      } 

      public String getId() { 
       return id; 
      } 

      public String getTopic() { 
       return topic; 
      } 
     } 
    } 

使用這個類的序列化,這樣

ObjectMapper oMapper = new ObjectMapper(); 
     Jack jack = new Jack("abc", "tete", "serialize"); 
     Map<String, Object> map = oMapper.convertValue(jack, Map.class); 
     getCombinedMap(map); 
     try { 
      String json = oMapper.writer().withDefaultPrettyPrinter().writeValueAsString(map); 
      Log.d("json: ", json); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

,這裏是方法使用

private Map<String, Object> getCombinedMap(Map<String, Object> map){ 
     Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator(); 
     while (iterator.hasNext()){ 
      Map.Entry<String, Object> entry = iterator.next(); 
      if(entry.getValue() instanceof Map){ 
       Map<String, Object> childMap = (Map<String, Object>) entry.getValue(); 
       getCombinedMap(childMap); 
       getChildMapInserted(map, childMap); 
       map.remove(entry.getKey()); 
      } 
     } 
     return map; 
    } 

    private Map<String, Object> getChildMapInserted(Map<String, Object> map, Map<String, Object> childMap){ 
     Iterator<Map.Entry<String, Object>> iterator = childMap.entrySet().iterator(); 
     while (iterator.hasNext()){ 
      Map.Entry<String, Object> entry = iterator.next(); 
      map.put(entry.getKey(),entry.getValue()); 
     } 
     return map; 
    } 

我知道}這種是不好的事情,但這將符合你的目的。 這是我得到的輸出。

json:: { 
     "name" : "abc", 
     "id" : "tete", 
     "topic" : "serialize" 
} 

你可以把任何其他物體代替千斤頂,這將轉換它。 日誌用於日誌記錄,您可以刪除該行。

相關問題