2013-07-22 23 views
1

我有兩個非常相似的JAXB對象具有數百個字段的組,唯一的區別是這些對象位於不同的包中,因此編譯器會阻止我設置另一個類的值。 這裏看起來如何。將2等於JAXB對象轉換爲另一個

//employer sits in this package: com.beans.enrollment 
     bodyResponse.setEmployer((com.beans.external.groupresponse.EmployerType) 
sgCreateQuoteRequest.getRequest().getEmployer()); 

所以我在這裏得到編譯錯誤,需要大量的時間手動獲取/設置這些字段。

任何想法如何投這些對象?

回答

0

您可以使用JAXB API進行復制。這涉及將源數據包裝在JAXBSource的實例中,然後Unmarshaller可以從Source解組簡單地解組JAXBSource以將數據複製到第二模型。

DEMO CODE

演示

import javax.xml.bind.*; 
import javax.xml.bind.util.JAXBSource; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     // Create Input from Foo Model 
     forum17791487.foo.Root fooRoot = new forum17791487.foo.Root(); 
     fooRoot.setValue("Hello World"); 
     JAXBContext fooContext = JAXBContext.newInstance(forum17791487.foo.Root.class); 
     JAXBSource jaxbSource = new JAXBSource(fooContext, fooRoot); 

     // Unmarshal Foo Input to Bar Model 
     JAXBContext barContext = JAXBContext.newInstance(forum17791487.bar.Root.class); 
     Unmarshaller unmarshaller = barContext.createUnmarshaller(); 
     forum17791487.bar.Root barRoot = (forum17791487.bar.Root) unmarshaller.unmarshal(jaxbSource); 
     System.out.println(barRoot.getValue()); 
    } 

} 

輸出

Hello World 

JAVA模型

以下類僅在軟件包名稱上有所不同。雖然在這個例子中每個包只使用了一個類,但同樣的原則適用於較大的模型。

forum17791487.foo.Root

package forum17791487.foo; 

import javax.xml.bind.annotation.*; 

@XmlRootElement 
public class Root { 

    private String value; 

    public String getValue() { 
     return value; 
    } 

    public void setValue(String foo) { 
     this.value = foo; 
    } 

} 

forum17791487.bar.Root

package forum17791487.bar; 

import javax.xml.bind.annotation.*; 

@XmlRootElement 
public class Root { 

    private String value; 

    public String getValue() { 
     return value; 
    } 

    public void setValue(String foo) { 
     this.value = foo; 
    } 

} 

瞭解更多信息

0

你不能施放它們。您可以使用一些反射代碼將值從一個實例複製到另一個實例。

+0

我該怎麼做?你有任何代碼示例? – user2508615

+0

@ user2508615 - 有大量的例子可以在線進行java反射。 – jtahlborn

0

一個「瘋狂」的想法可以使用GSon library。將A類型的對象轉換爲JSON,然後將JSON轉換爲B類型的對象。

如果A和B具有相同名稱的字段,則設置該值,否則將跳過。

A a = .... 
    Gson gson = new Gson(); 
    String json = gson.toJson(a); 
    B b = gson.fromJson(json, B.class); 
0

我不認爲java支持這種功能,對於鑄造兩個對象,它們必須在同一個層次結構中。

在您的場景中,您可以編寫一些將值映射到源地址的映射器,但如果像僱主這樣的多個類包含相同類型的字段,則可以使用兩種最終映射代碼的方法。

a)使用Dozer Mapping .

b)或在編譯時產生一個映射,即會做的映射從源到目的地,反之亦然。

注意:我已經對我的項目使用了兩種方法,但方法1只有一個缺點,就是它有一些性能瓶頸,因爲它使用反射進行映射。另一方面,在方法(b)中,編譯後生成了映射器,這是純粹的基於getter和setter的映射。 (沒有性能瓶頸)

+0

我剛纔看了一下推土機,發現我怎樣才能將POJO映射到JAXB,但我怎樣才能將JAXB映射到JAXB? – user2508615

+0

直到我的理解,沒有什麼像POJO到JAXB或JAXB到JAXB ..在java中的一切都是POJO,Dozer支持POJO到POJO映射沒有任何麻煩。 你的僱主版本都只是pojo(只有它的版本可能有用於模式生成的jaxb註釋)......所以它適用於你的情況。 – saurav

0

Dozer項目,這可能會幫助你(注:我還沒有使用它)。

1

我承認,推土機是輝煌的解決了這個問題。我試過了。而當你在春天創建一個bean,如:

@Bean 
    public Mapper getDozer(){ 
    return new DozerBeanMapper(); 
} 

,那麼你只注入映射器和包裝你的對象:

private org.project.GetDocuments convertDocsInput(org.external.GetDocuments input) { 
    return mapper.map(input, org.project.GetDocuments.class); 
} 
0

使用Apache的commons-BeanUtils的,你可以在2線做。

EmployerType targetType=com.beans.external.groupresponse.ObjectFactory.createEmployerType(); 
bodyResponse.setEmployer(
BeanUtils.copyProperties(targetType,sgCreateQuoteRequest.getRequest().getEmployer())); 

我跟着java-object-copy-using-beanutils中提到的例子來解決我的項目中的類似問題。

相關問題