如何获取 API 的 XML 响应,该 api 返回由对象列表组成的响应实体



我在 Spring 启动中编写了一个 REST API。API 返回对象列表作为响应。我从 API 的控制器类返回一个响应实体对象。但是我得到的 XML 输出格式不正确。我正在使用com.fasterxml.jackson.dataformat库。

该项目的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>sampleapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

我的控制器类是:

测试控制器.java

package com.example.sampleapi.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.sampleapi.Entity.Student;
import com.example.sampleapi.Entity.StudentResponse;
import com.example.sampleapi.dao.StudentDAO;

@RestController
public class TestController {
@Autowired
private StudentDAO studentDao;

@RequestMapping(value = "/save", method =RequestMethod.GET,
produces = {
MediaType.APPLICATION_XML_VALUE})
public boolean saveAllStudents() {
return studentDao.saveStudents();
}
@RequestMapping(value = "/get", method =RequestMethod.GET, produces = {
MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<StudentResponse> getAllStudents() {
List<Student> studentList = studentDao.getAllStudent();
int count = studentList.size();
StudentResponse studentResponse = new StudentResponse();
studentResponse.setStudentList(studentList);
studentResponse.setCount(count);
return new ResponseEntity<StudentResponse>(studentResponse, HttpStatus.OK);
}
}

我的DAO类是:

学生道.java

package com.example.sampleapi.dao.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.example.sampleapi.Entity.Student;
import com.example.sampleapi.dao.StudentDAO;

@Repository
public class StudentDAOImpl implements StudentDAO {
private List<Student> studentDetails = new ArrayList<>();
@Override
public boolean saveStudents() {
Student student1 = new Student();
student1.setName("A");
student1.setAge(23);
student1.setMarks(100);
Student student2 = new Student();
student2.setName("B");
student2.setAge(25);
student2.setMarks(90);
Student student3 = new Student();
student3.setName("C");
student3.setAge(19);
student3.setMarks(95);
studentDetails.add(student1);
studentDetails.add(student2);
studentDetails.add(student3);
if(studentDetails.size() > 0) {
return true;
} else {
return false;
}
}
@Override
public List<Student> getAllStudent() {
return studentDetails;
}
}

学生实体类如下所示:

学生.java

package com.example.sampleapi.Entity;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Student {
private String name;
private int age;
private double marks;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getMarks() {
return marks;
}
public void setMarks(double marks) {
this.marks = marks;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", marks=" + marks + "]";
}
}

响应类如下所示:

学生回应.java

package com.example.sampleapi.Entity;
import java.util.List;
public class StudentResponse {
int count;
List<Student> studentList;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}

}

/getRequestMapping 的 Xml 响应是:

<StudentResponse>
<count>3</count>
<studentList>
<studentList>
<name>A</name>
<age>23</age>
<marks>100.0</marks>
</studentList>
<studentList>
<name>B</name>
<age>25</age>
<marks>90.0</marks>
</studentList>
<studentList>
<name>C</name>
<age>19</age>
<marks>95.0</marks>
</studentList>
</studentList>
</StudentResponse>  

为什么列表中的学生对象是<studentList></studentList>的,它应该是<student></student>的?

我期待的XML格式是:

<StudentResponse>
<count>3</count>
<studentList>
<student>
<name>A</name>
<age>23</age>
<marks>100.0</marks>
</student>
<student>
<name>B</name>
<age>25</age>
<marks>90.0</marks>
</student>
<student>
<name>C</name>
<age>19</age>
<marks>95.0</marks>
</student>
</studentList>
</StudentResponse> 

您获得外部studentList的原因是,这是该列表在StudentResponse中的属性名称。

你得到内在studentList的原因是

当集合属性仅使用 @XmlElement 进行注释时[正如在 Spring Boot 中假设的那样,当你没有任何注释时],集合中的每个项目都将被一个元素包装。 源

您需要在 Java 类上放置注释才能获得所需的结果,在本例中如下所示

public class Customer {
@XmlElementWrapper(name="studentList")
@XmlElement(name="student")
private List<Student> studentList;
}

试试这个

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "student")
public class Student {
...
}

相关内容

  • 没有找到相关文章

最新更新