gpt4 book ai didi

java - Jersey 休息服务不返回 XMLresponse

转载 作者:行者123 更新时间:2023-11-30 08:55:58 24 4
gpt4 key购买 nike

我正在使用 Jersey 构建一个 rest API,其中允许根据客户端喜欢的格式输出 XML 和 JSON(使用 Accept header ) .该服务将以下类作为输出发送,如下所示

@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;
}
}

我不想将附件字段作为输出,所以用@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() {

但它仍然不起作用..我错过了什么吗?

最佳答案

我对您的 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;
}

}

你可以使用下面的适配器类

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;
}

}

关于java - Jersey 休息服务不返回 XMLresponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28805494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com