spring boot XML正在填充额外的标签

我正在尝试为雇员创建一个XML,但我的Spring Boot应用程序正在返回额外的标签,我在pom中添加了Jackson-dataformat-xml。我已经在pom中添加了Jackson-dataformat-xml。请帮助我,先谢谢你。

期待。

<Employees>
<Employee>
<id>1</id>
<name>Topic1</name>
<department>Description1</department>
</Employee>
</Employees>

实际结果:

<Employees>
<employees>
<employees>
<id>1</id>
<name>Topic1</name>
<department>Description1</department>
</employees>
</employees>
</Employees>

雇员类

public class Employees {

    List<Employee> employees = new ArrayList<Employee>();

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

    public Employees(List<Employee> employees) {
        super();
        this.employees = employees;
    }


}

员工类。

public class Employee {

    private int id;
    private String name;
    private String department;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public Employee(int id, String name, String department) {
        super();
        this.id = id;
        this.name = name;
        this.department = department;
    }

}

Controller.I am new to springboot and following some tutorial like java brains.I am trying to create a XML for employees but My Spring Boot application is returning additional tag:

@RestController
public class EmployeeController {

    @RequestMapping(value = "/employees", produces = "application/xml")
    public Employees getAllEmployees() {
        List<Employee> controllerEmployee = new ArrayList<Employee>();

        controllerEmployee.add(new Employee(1,"Topic1","Description1"));

        Employees employees = new Employees(controllerEmployee);

        return employees;
    }
}

解决方案:

您需要按照建议在您的 Employees 类中使用以下注解 @JacksonXmlElementWrapper(useWrapping = false) 。此处

@XmlRootElement
public class Employees {

@JacksonXmlProperty(localName = "Employee")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Employee> employees;

public List<Employee> getEmployees() {
    return employees;
}

public void setEmployees(List<Employee> employees) {
    this.employees = employees;
}

public Employees(List<Employee> employees) {
    this.employees = employees;
}

}

而这是你修改代码后得到的回复。

<Employees>
  <Employee>
   <id>1</id>
   <name>Employee1</name>
   <department>Dev</department>
  </Employee>
  <Employee>
   <id>2</id>
   <name>Employee2</name>
   <department>QA</department>
  </Employee>
</Employees>

给TA打赏
共{{data.count}}人
人已打赏
未分类

实时数据库cpu使用率%100

2022-9-13 14:51:35

未分类

Java如何给文件添加新行[重复]?

2022-9-13 14:51:37

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索