【问题标题】:Jersey rest service not returning XMLresponse泽西休息服务不返回 XMLresponse
【发布时间】:2015-05-02 13:13:47
【问题描述】:

我正在使用Jersey 构建一个rest API,其中允许XML and JSON 输出取决于客户端喜欢的格式(使用Accept 标头)。服务将以下类作为输出发送,如下所示

@XmlRootElement
public class ProjectDetails{

    private List<Attachment> attachments;
    private Map<String, List<Attachment>> imageCategory;

    @XmlTransient
    public List<Attachment> getAttachments() {
        return attachments;
    }

    public void setAttachments(List<Attachment> attachments) {
        this.attachments = attachments;
    }

    public Map<String, List<Attachment>> getImageCategory() {
        if(attachments == null || attachments.size() == 0){
            return null;
        }

        Map<String, List<Attachment>> map = new HashMap<String, List<Attachment>>();
        for (Attachment img : attachments){
            String key = img.getCategory();
            if(BaseUtil.hasText(key)){
                List<Attachment> values = map.get(key);
                if (values == null){
                    values = new ArrayList<Attachment>();
                }
                values.add(img);
                map.put(key, values);
            }
        }

        this.imageCategory = map ;
        return imageCategory;
    }

    public void setImageCategory(Map<String, List<Attachment>> imageCategory) {
        this.imageCategory = imageCategory;
    }
}

我不想将attachments field 作为输出,所以用@XmlTransient 标记它,我想使用附件字段形成Map 并将其作为输出发送。

JSON 格式的情况下,我得到了正确的响应。但在XML 的情况下,当我点击服务时,我没有得到任何输出。

我认为它与此 Map 字段有关,因为如果我删除 Map 字段并添加一些其他字段(如 String),那么我会得到该字段作为响应。

请告诉我如何解决这个问题。

更新: 经过一番谷歌搜索,我找到了XmlAdapter 解决方案并实现如下

public class MapAdapter extends
        XmlAdapter<MapAdapter.AdaptedMap, Map<String, List<Attachment>>> {

    public static class AdaptedEntry {
        public String key;
        public List<Attachment> value = new ArrayList<Attachment>();
    }

    public static class AdaptedMap {
        List<AdaptedEntry> entries = new ArrayList<AdaptedEntry>();
    }

    @Override
    public AdaptedMap marshal(Map<String, List<Attachment>> map)
            throws Exception {
        AdaptedMap adaptedMap = new AdaptedMap();

        for (Entry<String, List<Attachment>> entry : map.entrySet()) {
            AdaptedEntry adaptedEntry = new AdaptedEntry();
            adaptedEntry.key = entry.getKey();
            adaptedEntry.value = entry.getValue();
            adaptedMap.entries.add(adaptedEntry);
        }
        return adaptedMap;
    }

    @Override
    public Map<String, List<Attachment>> unmarshal(AdaptedMap adaptedMap)
            throws Exception {
        List<AdaptedEntry> adapatedEntries = adaptedMap.entries;
        Map<String, List<Attachment>> map = new HashMap<String, List<Attachment>>(
                adapatedEntries.size());

        for (AdaptedEntry adaptedEntry : adapatedEntries) {
            map.put(adaptedEntry.key, adaptedEntry.value);
        }
        return map;
    }

然后

@XmlJavaTypeAdapter(MapAdapter.class)
    public Map<String, String> getImageCategory() {

但它仍然无法正常工作..我错过了什么?

【问题讨论】:

标签: java xml json jaxb jersey-2.0


【解决方案1】:

我对您的ProjectDetails 类进行了一些更改,它提供了对XML 和JSON 的响应。你可以试试这个吗?

@XmlRootElement
public class ProjectDetails {
private List<Attachment> attachments;
private Map<String, ArrayList<Attachment>> imageCategory;

@XmlTransient
public List<Attachment> getAttachments() {
    return attachments;
}

public void setAttachments(List<Attachment> attachments) {
    this.attachments = attachments;
}
@XmlJavaTypeAdapter(MapAdapter.class) 
public Map<String, ArrayList<Attachment>> getImageCategory() {
    if(attachments == null || attachments.size() == 0){
        return null;
    }
    Map<String, ArrayList<Attachment>> map = new HashMap<String, ArrayList<Attachment>>();
    for (Attachment img : attachments){
        String key = img.getCategory();
        if(!key.equals("")){
            ArrayList<Attachment> values = map.get(key);
            if (values == null){
                values = new ArrayList<Attachment>();
            }
            values.add(img);
            map.put(key, values);
        }
    }

    this.imageCategory = map ;
    return imageCategory;
}

public void setImageCategory(Map<String, ArrayList<Attachment>> imageCategory) {
    this.imageCategory = imageCategory;
}

}

而Adapter类你可以使用下面的

public class MapAdapter extends XmlAdapter<MapElement[], Map<String, ArrayList<Attachment>>>{
public MapElement[] marshal(Map<String, ArrayList<Attachment>> arg0) throws Exception {
    MapElement[] mapElements = new MapElement[arg0.size()];
    int i = 0;
    for (Map.Entry<String, ArrayList<Attachment>> entry : arg0.entrySet()){
        mapElements[i++] = new MapElement(entry.getKey(), entry.getValue());
    }
    return mapElements;
}

public Map<String, ArrayList<Attachment>> unmarshal(MapElement[] arg0) throws Exception {
    Map<String, ArrayList<Attachment>> r = new HashMap<String, ArrayList<Attachment>>();
    for (MapElement mapelement : arg0)
        r.put(mapelement.key, mapelement.value);
    return r;
}

}

我也更改了 MapElement

public class MapElement {
@XmlElement
public String key;
@XmlElement
public ArrayList<Attachment> value;

private MapElement() {
}

public MapElement(String key, ArrayList<Attachment> value) {
    this.key = key;
    this.value = value;
}
}

并且 Attachement 类应该有 getter setter 方法

public class Attachment {
public String getCategory() {
    return category;
}
public void setCategory(String category) {
    this.category = category;
}
private String category;
public Attachment(String cat){
    this.category = cat;
}

}

【讨论】:

  • 值没有出现..我可以在响应中看到字符串键,但相应的值是空白
  • 嗨 Anand,我已经更新了您可能会查看的答案。
猜你喜欢
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
  • 2018-06-19
相关资源
最近更新 更多