使用RestTemplate获取XML对象列表返回空列表的问题排查
问题排查与解决方案
导致空列表的核心原因及修复步骤
1. ArrayOfObject类中的变量名与方法名不匹配
你的ArrayOfObject类中,字段定义为private List<Object> object;,但getter方法却返回objects(一个不存在的变量),这会直接导致返回null,最终呈现为空列表。同时需要配合Jackson XML注解指定XML元素名称。
修复后的ArrayOfObject类:
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import java.util.ArrayList; import java.util.List; public class ArrayOfObject { @JacksonXmlElementWrapper(useWrapping = false) @JacksonXmlProperty(localName = "Object") // 匹配XML中的<Object>元素 private List<MyObject> objects; // 统一使用复数命名更清晰 public ArrayOfObject() { objects = new ArrayList<>(); } public List<MyObject> getObjects() { return objects; } public void setObjects(List<MyObject> objects) { this.objects = objects; } }
2. Object类的注解与命名问题
Object是Java的关键字,作为类名会引发潜在的映射冲突,建议重命名为MyObject@JsonIgnoreProperties需要显式设置ignoreUnknown = true才能忽略XML中的var1/var2/var3字段
修复后的MyObject类:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) // 忽略未知字段 public class MyObject { public String id, name; public MyObject() {} public MyObject(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
3. RestController的更新
需要同步修改返回类型为List<MyObject>,并确保填入有效的API地址:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping public class RestCall { @GetMapping("/objects") public List<MyObject> getObject(){ final String uri = "你的外部API地址"; // 填入实际地址 RestTemplate rt = new RestTemplate(); ArrayOfObject result = rt.getForObject(uri, ArrayOfObject.class); return result != null ? result.getObjects() : new ArrayList<>(); } }
手动循环添加对象的实现方案
如果不想依赖Jackson自动映射,可以手动解析XML并循环添加对象:
import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.StringReader; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping public class RestCall { @GetMapping("/objects") public List<MyObject> getObject() throws Exception { final String uri = "你的外部API地址"; RestTemplate rt = new RestTemplate(); String xmlResponse = rt.getForObject(uri, String.class); List<MyObject> objects = new ArrayList<>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xmlResponse))); // 循环遍历所有<Object>元素 NodeList nodeList = doc.getElementsByTagName("Object"); for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); String id = element.getElementsByTagName("id").item(0).getTextContent(); String name = element.getElementsByTagName("name").item(0).getTextContent(); objects.add(new MyObject(id, name)); } return objects; } }
内容的提问来源于stack exchange,提问作者Matt
相关产品推荐
相关产品推荐

