我正在尝试从XSD文件生成java类,其中包含Map<String, Boolean
。我已经阅读了教程(http://todayguesswhat.blogspot.co.uk/2012/09/jaxb-xsd-to-java-maphashmap-example.html),其中说我必须使用适配器和绑定才能获得所需的结果。
但出于某种原因,生成属性为List
而不是Map
。你能帮我解决一下我的错误吗?
谢谢!
我的XSD架构:
<xs:element name="AutoCompleteReq">
<xs:complexType>
<xs:sequence>
<xs:element name="queryString" type="xs:string"/>
<xs:element name="boostingFactors" type="test:StringBooleanMapModeller" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="StringBooleanMapModeller">
<xs:sequence>
<xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="key" type="xs:string"/>
<xs:element name="value" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
我的绑定文件:
<jaxb:bindings schemaLocation="AutoCompleteReq.xsd">
<jaxb:bindings node="//xs:element[@name='AutoCompleteReq']//xs:element[@name='boostingFactors']">
<jaxb:property>
<jaxb:baseType name="com.company.support.jaxb.StringBooleanMap<String,Boolean>" />
</jaxb:property>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
StringBooleanMap.java
package com.company.support.jaxb;
import java.util.HashMap;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlJavaTypeAdapter(StringBooleanMapAdapter.class)
public class StringBooleanMap<String, Boolean> extends HashMap<String, Boolean> {
}
StringBooleanMapAdapter.java
package com.company.support.jaxb;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import com.company.xml.representation.StringBooleanMapModeller;
public class StringBooleanMapAdapter extends XmlAdapter<StringBooleanMapModeller, HashMap<String, Boolean>> {
@Override
public HashMap<String, Boolean> unmarshal(StringBooleanMapModeller v)
throws Exception {
HashMap<String, Boolean> map = new HashMap<String, Boolean>();
for(StringBooleanMapModeller.Entry e : v.getEntry())
{
map.put(e.getKey(), e.isValue());
}
return map;
}
@Override
public StringBooleanMapModeller marshal(HashMap<String, Boolean> v)
throws Exception {
StringBooleanMapModeller modeller = new StringBooleanMapModeller();
for(Map.Entry<String, Boolean> entry : v.entrySet())
{
StringBooleanMapModeller.Entry e = new StringBooleanMapModeller.Entry();
e.setKey(entry.getKey());
e.setValue(entry.getValue());
modeller.getEntry().add(e);
}
return modeller;
}
}
pom.xml(XSD生成片段)
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>Representations</id>
<configuration>
<schemaDirectory>XSD/Representation</schemaDirectory>
<packageName>com.company.xml.representation</packageName>
<bindingDirectory>XSD/Representation</bindingDirectory>
<outputDirectory>src/main/generated-sources</outputDirectory>
<staleFile>${project.build.directory}/generated-sources/jaxb/.representation</staleFile>
<clearOutputDir>false</clearOutputDir>
</configuration>
<goals>
<goal>xjc</goal>
</goals>
</execution>
<execution>
<id>Events</id>
<configuration>
<schemaDirectory>XSD/Representation/Event</schemaDirectory>
<packageName>com.company.xml.representation.event</packageName>
<bindingDirectory>XSD/Representation/Event</bindingDirectory>
<outputDirectory>src/main/generated-sources</outputDirectory>
<staleFile>${project.build.directory}/generated-sources/jaxb/.event</staleFile>
<clearOutputDir>false</clearOutputDir>
</configuration>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
</plugin>
答案 0 :(得分:2)
从StringBooleanMap后面删除<String, Boolean>
,它不会按照您的想法执行。 String和Boolean被解释为通用参数,而不是java.lang.String和java.lang.Boolean。
正确:
package com.company.support.jaxb;
import java.util.HashMap;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlJavaTypeAdapter(StringBooleanMapAdapter.class)
public class StringBooleanMap extends HashMap<String, Boolean> {
}
您的代码被解释为:
public class StringBooleanMap<S,B> extends HashMap<S,B> {
另外,将jaxb:baseType引用更改为:
<jaxb:baseType name="com.company.support.jaxb.StringBooleanMap" />