JAXB没有';在将java从1.8.0_77更新到1.8.0_121后,t解组
昨天我更新了标题中的java,现在JAXB不再解析xml了。所有对象都是空的,似乎没有设置任何内容 给定此POJO-列表匹配产品响应JAXB没有';在将java从1.8.0_77更新到1.8.0_121后,t解组,java,xml,jaxb,Java,Xml,Jaxb,昨天我更新了标题中的java,现在JAXB不再解析xml了。所有对象都是空的,似乎没有设置任何内容 给定此POJO-列表匹配产品响应 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "ListMatchingProductsResponse", propOrder = { "listMatchingProductsResult", "responseMetadata" }) @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ListMatchingProductsResponse", propOrder = {
"listMatchingProductsResult",
"responseMetadata"
})
@XmlRootElement(name = "ListMatchingProductsResponse")
public class ListMatchingProductsResponse {
@XmlElement(name = "ListMatchingProductsResult")
private ListMatchingProductsResult listMatchingProductsResult;
@XmlElement(name = "ResponseMetadata")
private ResponseMetadata responseMetadata;
@XmlAttribute(name = "xmlns")
private String xmlns;
public String getXmlns() {
return xmlns;
}
public void setXmlns(String xmlns) {
this.xmlns = xmlns;
}
/**
* Get the value of ListMatchingProductsResult.
*
* @return The value of ListMatchingProductsResult.
*/
public ListMatchingProductsResult getListMatchingProductsResult() {
return listMatchingProductsResult;
}
/**
* Set the value of ListMatchingProductsResult.
*
* @param listMatchingProductsResult The new value to set.
*/
public void setListMatchingProductsResult(ListMatchingProductsResult listMatchingProductsResult) {
this.listMatchingProductsResult = listMatchingProductsResult;
}
/**
* Get the value of ResponseMetadata.
*
* @return The value of ResponseMetadata.
*/
public ResponseMetadata getResponseMetadata() {
return responseMetadata;
}
/**
* Set the value of ResponseMetadata.
*
* @param responseMetadata The new value to set.
*/
public void setResponseMetadata(ResponseMetadata responseMetadata) {
this.responseMetadata = responseMetadata;
}
}
以及ListMatchingProductsResult
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="ListMatchingProductsResult", propOrder={
"products"
})
@XmlRootElement(name = "ListMatchingProductsResult")
public class ListMatchingProductsResult {
@XmlElement(name="Products")
private ProductList products;
/**
* Get the value of Products.
*
* @return The value of Products.
*/
public ProductList getProducts() {
return products;
}
/**
* Set the value of Products.
*
* @param products
* The new value to set.
*/
public void setProducts(ProductList products) {
this.products = products;
}
/**
* Check to see if Products is set.
*
* @return true if Products is set.
*/
public boolean isSetProducts() {
return products != null;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("products", products)
.toString();
}
}
我使用它来解组:
String s = getResult();
// s is the expected xml string
ListMatchingProductsResponse m = JAXB.unmarshal(new StringReader(s), ListMatchingProductsResponse.class);
log.info(m); // ListMatchingProductsResponse@7164e54
log.info(m.getListMatchingProductsResult()); // null
我有点不知所措,因为我看不出任何原因,也没有在变更日志中找到关于JAXB的任何更改
我做错了什么
到目前为止,以下答案并没有解决我的问题
更新
现在,我的项目中包含了以下依赖项
group: 'org.jvnet.jaxb2.maven2', name: 'maven-jaxb2-plugin', version: '0.13.1'
它又起作用了。这是一个新问题——为什么?121 java版本中是否有遗漏/缺陷?我已经编辑了这篇文章,因为事实证明,JRE中的更改在技术上并不是一个缺陷,但1.8u91和以前的版本更为宽松,允许使用无效的名称空间XML,如果XML没有正确的名称空间,则1.8u101+会中断 我在上创建了一个示例来说明差异。尝试使用1.8u91和1.8u101+运行两个主电源
NoSchema
和WithSchema
,以查看性能差异
在我的例子中,XML不包含默认名称空间,但元素的前缀不是它们所属的名称空间(
broker.XML
)。这在1.8u91中运行良好,但在1.8u101中失败。虽然升级破坏了我们的代码,但从技术上讲,这不是Oracles的错误,因为XML的名称空间不正确。我遇到了在jdk 101+版本中给空值的解组问题,并通过包含包信息.java
和注释@javax.XML.bind.annotation.XmlSchema
下面是我的代码
@javax.xml.bind.annotation.XmlSchema(elementFormDefault=XmlNsForm.QUALIFIED,名称空间=”http://example.com/api
”)
包org.example
import javax.xml.bind.annotation.XmlNsForm;
Java8较新的版本更严格,我也有同样的问题并发现,我们必须为每个字段指定名称空间,如下所示进行验证 它对我有用
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ListMatchingProductsResponse", propOrder = {
"listMatchingProductsResult",
"responseMetadata"
})
@XmlRootElement(name = "ListMatchingProductsResponse", namespace="http://example.com/api)
public class ListMatchingProductsResponse {
@XmlElement(name = "ListMatchingProductsResult", namespace="http://example.com/api)
private ListMatchingProductsResult listMatchingProductsResult;
@XmlElement(name = "ResponseMetadata", namespace="http://example.com/api)
private ResponseMetadata responseMetadata;
@XmlAttribute(name = "xmlns", namespace="http://example.com/api)
private String xmlns;
当使用从elementFormDefault
属性设置为qualified
的模式生成的类来解组基本的、无名称空间的XML时,我遇到了一个难题。简单地使用(默认)unqualified
值就解决了我的问题:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified">
...
</xs:schema>
...
maven-jaxb2-plugin
是一个用于代码生成的maven插件,您不应该将其作为编译或运行时依赖项包含。@lexicore我尝试了几种不同的jaxb依赖项,都不起作用,但包括上面的一种。虽然我知道它没有任何意义,但我想知道为什么包含它会让它再次工作…maven-jaxb2-plugin
本身就依赖于JAXB库。因此,基本上您从Maven插件“继承”依赖项(而不是自己拥有它们)。请参见:JAXB也有类似的问题。第一次注意到它是在jdk1.8.0_102中。以前没有必要调查和修复我也有类似的问题。我从1.8.0_91升级到1.8.0_121(以获得letsencrypt证书支持),但_121无法解析我的xml(所有字段均为空)。我尝试过用两种JDK生成,用两种JDK解析,但用_121解析总是失败。这听起来像是_121的解析问题,因为_91可以解析XML,即使类是用_121生成的。当我试图构建一个独立的示例时,我发现了以下内容:`jaxb2 maven plugin`创建了一个package info.java
和一个ObjectFactory.java。如果我删除包信息(或其@XmlSchema注释),那么Jaxb也可以在u101+上工作。所以,也许旧版本更宽松,而新版本更严格。拯救生命的答案-谢谢。当我们升级到JRE 1.8.101时,我的对象解组开始失败,并且没有异常被打印到日志中。