【问题标题】:Marshall/Unmarshall MapMarshall/Unmarshall 地图
【发布时间】:2020-12-18 06:43:13
【问题描述】:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemSubstitutionRequestDTO {
    
    public ItemSubstitutionRequestDTO()
    {
        
    }
    
    private List<Map<String,Integer>> substituteFor=new ArrayList<Map<String,Integer>>();
  private String orderId;
  
  public List<Map<String,Integer>> getSubstituteFor()
  {
      return substituteFor;
  }
  
  public void setSubstituteFor(List<Map<String,Integer>> substituteFor)
  {
      this.substituteFor = substituteFor;
  }
  
  public String getOrderId() {
      return orderId;
  }

  public void setOrderId(String orderId) {
      this.orderId = orderId;
  }
  
}

最终结果错误

java.util.Map是一个接口,JAXB不能处理接口。

我无法让 JaxB 编组/解组 Map 的实例。我也尝试了其他注释,发现这是解决上述错误的可能方法之一,但没有任何反应。

下面是来自UI端的输入json

{ "itemSubstitutionRequestDTO": { "substituteFor": [{"41712":2}], "orderId": "1073901", } }

【问题讨论】:

  • 您的示例是 JSON,而不是 XML。 JAXB 仅适用于 XML。对于 JSON,您需要另一个框架,可能是 Jackson。

标签: java xml jaxb marshalling unmarshalling


【解决方案1】:

您没有将您的 XML 内容写入 &lt;substituteFor&gt; 元素看起来像。 因此我假设是这样的:

<itemSubstitutionRequestDTO>
    <substituteFor>
        <item>
            <key>x</key>
            <value>23</value>
        </item>
        <item>
            <key>y</key>
            <value>3</value>
        </item>
    </substituteFor>
    <orderId>abc</orderId>
</itemSubstitutionRequestDTO>

正如 JAXB 错误消息已经告诉您的那样, 它无法处理具有&lt; &gt; 之间接口的类型, 比如你的List&lt;Map&lt;String,Integer&gt;&gt;。 但是它可以处理具有&lt; &gt; 之间普通类的类型, 喜欢List&lt;SubstitutionMap&gt;

所以第一步是重写你的ItemSubstitutionRequestDTO 类 所以它不使用List&lt;Map&lt;String,Integer&gt;&gt;,而是使用List&lt;SubstitutionMap&gt;。 您需要自己编写SubstitutionMap 类(不是接口)。 但这可能非常简单:

public class SubstitutionMap extends HashMap<String, Integer> {
}

现在 JAXB 不再抛出错误,但它仍然不知道如何编组/解组 SubstitutionMap。 因此你需要为它写一个XmlAdapter。 我们称之为SubstitutionMapAdapter。 要让 JAXB 知道这个适配器,您需要注释 substituteFor ItemSubstitutionRequestDTO 类中的属性:

@XmlJavaTypeAdapter(SubstitutionMapAdapter.class)

适配器的工作是从SubstitutionMap 进行实际转换 到SubstitutionMapElements 的数组,反之亦然。 然后 JAXB 就可以自己处理SubstitutionMapElement 数组了。

public class SubstitutionMapAdapter extends XmlAdapter<SubstitutionMapElement[], SubstitutionMap> {

    @Override
    public SubstitutionMap unmarshal(SubstitutionMapElement[] elements) {
        if (elements == null)
            return null;
        SubstitutionMap map = new SubstitutionMap();
        for (SubstitutionMapElement element : elements)
            map.put(element.getKey(), element.getValue());
        return map;
    }

    @Override
    public SubstitutionMapElement[] marshal(SubstitutionMap map) {
        // ... (left to you as exercise)
    }
}

SubstitutionMapElement 类只是键和值的简单容器。

@XmlAccessorType(XmlAccessType.FIELD)
public class SubstitutionMapElement {

    private String key;
    private int value;
    
    // ... constructors, getters, setters omitted here for brevity
}

【讨论】:

  • 嘿,对不起,我没有提供输入......我已经更新了来自 UI 端的输入
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多