8

我正在尝试从包含Map<String, Boolean. 我已经阅读了教程(http://todayguesswhat.blogspot.co.uk/2012/09/jaxb-xsd-to-java-maphashmap-example.html),它说我必须使用适配器和绑定才能实现期望的结果。

但是由于某种原因后生成属性是 aList而不是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&lt;String,Boolean&gt;" />
   </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>
4

1 回答 1

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" />
于 2014-01-16T16:23:57.933 回答