2011-08-06 130 views
9

I am using some non-standard extensions來自EclipseLink的JAXB實現,爲了實現這個實現,我必須使用jaxb.properties來配置它。效果很好。我可以用代碼替換jaxb.properties嗎?

但是,由於構建錯誤,屬性文件未包含在適當的位置,導致使用默認JAXB,而沒有任何錯誤只是繼續解析XML文件,忽略非標準擴展,留給我一個非工作的bean。

爲了使這更健壯,我想擺脫屬性文件並指定代碼中的上下文配置。我已經有了一個EclipseLink的編譯時間依賴關係,因爲它們有註釋,我不需要在部署時配置這個部分(事實上,看看會出現什麼問題,我不希望它是可配置的)。

+0

考慮一種替代方案:測試下旬在構建jaxb.properties的存在。 –

回答

12

你可以做下面的得到一個的EclipseLink JAXB(莫西)JAXBContext沒有jaxb.properties文件:

import java.io.File; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 

import org.eclipse.persistence.jaxb.JAXBContextFactory; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     //JAXBContext jc = JAXBContext.newInstance(Animals.class); 
     JAXBContext jc = JAXBContextFactory.createContext(new Class[] {Animals.class}, null); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     File xml = new File("src/forum6871469/input.xml"); 
     Animals animals = (Animals) unmarshaller.unmarshal(xml); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(animals, System.out); 
    } 

} 
+0

我如何在上面的代碼中設置MarshallerProperties.JSON_INCLUDE_ROOT屬性? – Vivek

相關問題