Spring Boot 使用RestTemplate获取XML对象列表会返回一个空列表

g6baxovj  于 2022-11-23  发布在  Spring
关注(0)|答案(1)|浏览(324)

我想知道如何调用一个外部API服务来获得我的类Map的对象列表。
XML如下所示

<ArrayOfObject>
    <Object>
        <id></id>
        <name></name>
        <var1></var1>
        <var2></var2>
        <var3></var3>
    </Object>
    <Object>
       <id></id>
       <name></name>
       <var1></var1>
       <var2></var2>
       <var3></var3>
   </Object>
  
   ...

</ArrayOfObject>

我试图只获取id和name,所以我试图在我的类中只Map这两个带有@JsonIgnoreProperties注解的字段:

@JsonIgnoreProperties
public class Object{
    public String id, name;
    
    public Object() {}
    public Object(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;
    }

并将其包成了

public class ArrayOfObject {

    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Object> object;
    
    public ArrayOfObject () {
        object = new ArrayList<>();
    }

    
    public List<Object> getObjects() {return objects;}
    public void setObjects(List<Object> objects) {
    this.object = objects;
    }   
}

这就是我的控制器

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;

@RestController
@RequestMapping
public class RestCall {

    @GetMapping("/objects")
    public List<Object> getObject(){
        final String uri = "";
        
    RestTemplate rt = new RestTemplate();
        
    ArrayOfObject result = rt.getForObject(uri, ArrayOfObject.class);
    return result.getObjects();
    }
}

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>it.me</groupId>
    <artifactId>Reading-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Reading-test</name>
    <description>Service</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我想知道为什么它返回一个空列表,我在序列化中遗漏了什么,或者可能是其他的东西?
使用for循环并将这些对象添加到列表中会更容易些?如果可以,我如何实现这样的东西?
这是previous question的一部分,但不是重复项。

pjngdqdw

pjngdqdw1#

不知道是否有人关心,但我已经解决了添加一些注解,现在看起来像:

package it.me.model;

import java.io.Serializable;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@JacksonXmlRootElement(localName = "Object")
public class Object implements Serializable{

    private static final long serialVersionUID = 1L;
    
    @JacksonXmlProperty
    private String id;
    
    @JacksonXmlProperty
    private String name;
    
    @JacksonXmlProperty
    private String var1;
    
    @JacksonXmlProperty
    private String var2;
    
    @JacksonXmlProperty
    private String var3;
    
    public Object() {}
    public Object(String id, String name, String var1, String var2, String var3) {
        this.id = id;
        this.name = name;
        this.var1= var1;
        this.var2= var2;
        this.var3= var3;
        
    }
    
    //getters n setters
    public static long getSerialversionuid() {return serialVersionUID;}
    
    
    //tostring, equals, hashcode overrides

}

这就是 Package 类:

package it.me.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@JacksonXmlRootElement
public class ArrayOfObject implements Serializable{

    private static final long serialVersionUID = 1L;

    @JacksonXmlProperty(localName = "Object")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Object> ArrayOfObject = new ArrayList<>();

    public ArrayOfObject() {}
    public ArrayOfObject(List<Object> arrayOfObject) {
        ArrayOfObject = arrayOfObject;
        
    }
    
    public List<Object> getArrayOfObject() {return ArrayOfObject;}
    public void setArrayOfObject(List<Object> arrayOfObject) {
        ArrayOfObject = arrayOfObject;
        
    }
    public static long getSerialversionuid() {return serialVersionUID;}

}

在我的pom.xml thoose需要:(不确定第二个)

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
</dependency>

最后是控制器:

package it.me.controller;

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 it.me.model.ArrayOfObject;

@RestController
@RequestMapping
public class Controller {
    
    @GetMapping("/objects")
    public ArrayOfObject getObjects(){

        final String uri = ""
        
        RestTemplate rt = new RestTemplate();
        
        ArrayOfObject result = rt.getForObject(uri, ArrayOfObject.class);
        return result;
    }

}

相关问题