在karaf 4.0.3中,不通过JAXB解析XML属性。相同的应用程序适用于Eclipse内部OSGI容器,但使用karaf容器失败。 在这两种情况下都使用JDK7 JAXB实现。
任何想法失败的原因?
传入XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<responsible version="1.0">
</responsible>
解析方法:
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(new Thread() {
public void run() {
try {
System.out.println("===============================================");
String name = "Responsible-response.xml";
Class<com.log4j2test.pojo.Responsible> baseClass = com.log4j2test.pojo.Responsible.class;
JAXBContext context = JAXBContext.newInstance(baseClass);
Unmarshaller u = context.createUnmarshaller();
final InputStream is = this.getClass().getResourceAsStream(name);
final InputStream is2 = this.getClass().getResourceAsStream(name);
String incomingXml = convertStreamToString(is2);
System.out.println("===Incoming XML: \n" + incomingXml);
StreamSource source = new StreamSource(is);
JAXBElement<?> unmarshaled = u.unmarshal(source, baseClass);
Object po = unmarshaled.getValue();
System.out.println("===Parsed POJO: " + po);
} catch (Throwable e) {
e.printStackTrace();
}
}
}, 5, 5, TimeUnit.SECONDS);
}
POJO:
@XmlRootElement(name = "responsible")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "responsible", propOrder = { "version" })
public class Responsible {
@XmlAttribute(name = "version")
protected Double version;
public Double getVersion() {
return version;
}
public void setVersion(Double version) {
this.version = version;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Responsible [");
builder.append("version=");
builder.append(version);
builder.append("]");
return builder.toString();
}
KARAF的输出:
Eclipse中的输出:
答案 0 :(得分:1)
添加javax.xml.bind解决了问题:
karaf/etc/config.properties:
org.osgi.framework.bootdelegation = \
javax.xml.bind, \