我获取了缺陷网址:
String defectUrl = conn.buildEntityCollectionUrl("defect");
defectUrl += "/98";
Map<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Accept", "application/xml");
Response res = conn.httpGet(defectUrl, null, requestHeaders);
然后转换为实体:
String postedEntityReturnedXml = res.toString();
Entity entity = EntityMarshallingUtils.marshal(Entity.class,postedEntityReturnedXml);
而不是更改字段值:
List<Field> fields = entity.getFields().getField();
for (Field f : fields) {
if (f.getName().equalsIgnoreCase("id"))
{
int i=f.hashCode();
System.out.println(i);
f.getValue().clear();
f.setName("");
}
}
现在尝试再次将其转换为xml formate:
String xml = EntityMarshallingUtils.unmarshal(org.eclipse.jetty.server.Response.class,**fields**);
System.out.println(xml);
抛出错误,因为对象字段不应该存在,并且无法得到我应该用什么代替字段?
我的EntityMarshallingUtils类就像:
package org.hp.qc.web.restapi.docexamples.docexamples.infrastructure;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.Marshaller;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
//import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class EntityMarshallingUtils {
private EntityMarshallingUtils() {}
@SuppressWarnings("unchecked")
public static <T> T marshal(Class<T> c, String xml) throws JAXBException {
T res;
if (c == xml.getClass()) {
res = (T) xml;
}
else {
JAXBContext ctx = JAXBContext.newInstance(c);
Unmarshaller marshaller = ctx.createUnmarshaller();
res = (T) marshaller.unmarshal(new StringReader(xml));
}
return res;
}
@SuppressWarnings("unchecked")
public static <T> String unmarshal(Class<T> c, Object o) throws Exception {
JAXBContext ctx = JAXBContext.newInstance(c);
Marshaller marshaller = ctx.createMarshaller();
StringWriter entityXml = new StringWriter();
marshaller.marshal(o, entityXml);
String entityString = entityXml.toString();
return entityString;
}
}
AnyOne谁可以帮我这个?