【发布时间】:2017-06-10 13:29:26
【问题描述】:
昨天我更新了标题中发布的java,现在JAXB不再解析xml了。所有对象都只是空,似乎没有设置。
鉴于此 POJO - ListMatchingProductsResponse
@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 的任何更改。
我在这里做错了什么?
到目前为止,以下答案并没有解决我的问题
JAXB Configuration was broken by upgrading from JDK 1.7 to JDK 1.8 u05 for collections
Unmarshalling jaxB doesn't work, objects null
Unmarshall with JAXB doesn't work
更新
我现在已将以下依赖项包含到我的项目中
group: 'org.jvnet.jaxb2.maven2', name: 'maven-jaxb2-plugin', version: '0.13.1'
它再次工作。这么新的问题——为什么会这样? 121 java 版本中是否缺少某些内容/错误?
【问题讨论】:
-
maven-jaxb2-plugin是一个用于代码生成的 Maven 插件,您不应该将其作为编译或运行时依赖项。 -
@lexicore 我尝试了几种不同的 jaxb 依赖项,但没有一个有效 - 但包括以上一个。虽然我知道它没有任何意义,但我想知道为什么包含它会使它再次工作......
-
maven-jaxb2-plugin本身依赖于 JAXB 库。所以基本上你从 Maven 插件“继承”依赖项(而不是自己拥有它们)。看到这个:stackoverflow.com/questions/26413431/… -
JAXB 有类似的问题。首先用jdk1.8.0_102注意到它。以前不需要调查和修复
-
我也有类似的问题。我正在从 1.8.0_91 升级到 1.8.0_121(以获得letsencrypt 证书支持),但_121 无法解析我的xml(所有字段均为空)。我尝试使用两个 JDK 生成,并使用两个 JDK 进行解析,但解析总是失败,_121。这听起来像是 _121 的解析问题,因为即使使用 _121 生成类,_91 也可以解析 XML。