2011-08-06 129 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

相关问题