地图的JAXB映射 [英] JAXB mapping for a map

查看:94
本文介绍了地图的JAXB映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JAXB,如何在下面映射< entry key =childResources>

Using JAXB, how to map <entry key="childResources"> below?

I尝试将其映射到Map,@XmlRootElement anotated类和其他方式的列表,但没有成功。

I tried mapping it to a Map, to a list of @XmlRootElement anotated classes and other ways, without success.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<map>    
  <entry key="extraProperties">        
    <map>            
      <entry key="methods">                
        <list>                    
          <map>                        
            <entry key="name" value="GET" />
          </map>                    
          <map />                    
          <map>                        
            <entry key="name" value="POST" />                        
            <entry key="messageParameters">                            
              <map>                                
                <entry key="id">                                    
                  <map>                                        
                    <entry key="acceptableValues" value="" />                                        
                    <entry key="optional" value="false" />                                        
                    <entry key="defaultValue" value="" />                                        
                    <entry key="type" value="string" />
                  </map>                                
                </entry>                                
                <entry key="enabled">                                    
                  <map>                                        
                    <entry key="acceptableValues" value="" />                                        
                    <entry key="optional" value="true" />                                        
                    <entry key="defaultValue" value="true" />                                        
                    <entry key="type" value="boolean" />
                  </map>                                
                </entry>                                
                <entry key="factoryclass">                                    
                  <map>                                        
                    <entry key="acceptableValues" value="" />                                        
                    <entry key="optional" value="false" />                                        
                    <entry key="defaultValue" value="" />                                        
                    <entry key="type" value="string" />
                  </map>                                
                </entry>                                
                <entry key="description">                                    
                  <map>                                        
                    <entry key="acceptableValues" value="" />                                        
                    <entry key="optional" value="true" />                                        
                    <entry key="defaultValue" value="" />                                        
                    <entry key="type" value="string" />
                  </map>                                
                </entry>                                
                <entry key="target">                                    
                  <map>                                        
                    <entry key="acceptableValues" value="" />                                        
                    <entry key="optional" value="true" />                                        
                    <entry key="defaultValue" value="server" />                                        
                    <entry key="type" value="string" />
                  </map>                                
                </entry>                                
                <entry key="property">                                    
                  <map>                                        
                    <entry key="acceptableValues" value="" />                                        
                    <entry key="optional" value="true" />                                        
                    <entry key="defaultValue" value="" />                                        
                    <entry key="type" value="string" />
                  </map>                                
                </entry>                                
                <entry key="restype">                                    
                  <map>                                        
                    <entry key="acceptableValues" value="" />                                        
                    <entry key="optional" value="false" />                                        
                    <entry key="defaultValue" value="" />                                        
                    <entry key="type" value="string" />
                  </map>                                
                </entry>                            
              </map>                        
            </entry>                    
          </map>                
        </list>            
      </entry>            
      <entry key="commands">                
        <list />
      </entry>            
      <entry key="childResources">                
        <map>                    
          <entry key="ab/cd" value="http://localhost:4848/management/domain/resources/custom-resource/ab%2Fcd" />                    
          <entry key="xx" value="http://localhost:4848/management/domain/resources/xx" />
        </map>            
      </entry>        
    </map>    
  </entry>    
  <entry key="message" value="" />    
  <entry key="exit_code" value="SUCCESS" />    
  <entry key="command" value="custom-resource" />
</map>


推荐答案

java.util.Map接口没有提供者marshal / unmarshal它的实例。您必须编写一个扩展 XmlAdapter 的类,并使用<$ c注释 java.util.Map 属性$ c> @XmlJavaTypeAdapter(MyMapAdapter.class)。

The java.util.Map interface has no provider to marshal/unmarshal its instances. You'll have to write a class that extends XmlAdapter and annotate the java.util.Map attribute with @XmlJavaTypeAdapter(MyMapAdapter.class).

在Google上寻找 jax-rs java一段时间后.util.map ,我最后得到的代码如下。在我的情况下,我将我的适配器命名为 XmlStringMapAdapter 而不是 MyMapAdapter

After a while on Google looking for jax-rs java.util.map, I ended up with the codes bellow. In my case I have named my adapter as XmlStringMapAdapter instead of MyMapAdapter.

ServiceDefinition.java

@XmlRootElement(name = "entry")
@XmlAccessorType(XmlAccessType.FIELD)
public class ServiceDefinition {

    @XmlAttribute
    private String key;

    @XmlJavaTypeAdapter(XmlStringMapAdapter.class)
    private Map<String, String> map = new HashMap<String, String>();

    // getters & setters
}

EntryElement.java

public class EntryElement {

    @XmlAttribute public String key;
    @XmlAttribute public String value;

    public EntryElement() {}

    public EntryElement(String key, String value) {
        this.key = key;
        this.value = value;
    }
}

MapElement.java

@XmlRootElement(name = "map")
public class MapElement {

    @XmlElement(name = "entry")
    public List<EntryElement> entries = new ArrayList<EntryElement>();

    public void addEntry(String key, String value) {
        entries.add(new EntryElement(key, value));
    }

}

XmlStringMapAdapter.java

public class XmlStringMapAdapter extends XmlAdapter<MapElement, Map<String, String>> {

    @Override
    public MapElement marshal(Map<String, String> v) throws Exception {

        if (v == null || v.isEmpty()) {return null;}

        MapElement map = new MapElement();

        for (String key : v.keySet()) {
            map.addEntry(key, v.get(key));
        }

        return map;
    }

    @Override
    public Map<String, String> unmarshal(MapElement v) throws Exception {
        if (v == null) {return null;}

        Map<String, String> map = new HashMap<String, String>(v.entries.size());

        for(EntryElement entry: v.entries) {
            map.put(entry.key, entry.value);
        }

        return map;
    }

}

而且bellow是我的课程用于测试新的地图适配器。

And bellow is a class that I used to test the new map adapter.

ServiceResouce.java

@Path("service")
public class ServiceResource {

    @Context
    private UriInfo uriInfo;

    @GET
    @Path("definition")
    @Produces("application/xml")
    public Response getDefinition() {
        ServiceDefinition def = new ServiceDefinition();
        UriBuilder b = uriInfo.getBaseUriBuilder();

        def.setKey("childResources");
        def.getMap().put("ab/cd", b.path("ab/cd").build(this).getPath());
        def.getMap().put("xx", b.path("xx").build(this).getPath());

        return Response.ok(def).build();
    }
}

上述资源的输出如下所示。

Output from the resource above is as shown bellow.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<entry key="childResources">
    <map>
        <entry key="ab/cd" value="http://localhost:8080/management/resources/ab/cd" />
        <entry key="xx" value="http://localhost:8080/management/resources/xx" />
    </map>
</entry>

这篇关于地图的JAXB映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆