【发布时间】:2021-06-04 14:17:43
【问题描述】:
我的 API 方法无法将对象转换为 XML,但它可以转换为 JSON。当方法返回类型为 Collection (MAP) 时,它会在没有任何运行时异常的情况下转换为 XML(未找到 MessageBodyWriter),但是当使用 Response 作为返回类型时,它会导致运行时异常。这里我提到了代码。我认为我的代码没有任何问题,但依赖项有问题。
package lk.ac.jfn.vau.MyDBapi.Model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Department {
private long Id;
private String Name;
private String Location;
public Department() {
}
public Department(long id, String name, String location) {
super();
Id = id;
Name = name;
Location = location;
}
public long getId() {
return Id;
}
public void setId(long id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getLocation() {
return Location;
}
public void setLocation(String location) {
Location = location;
}
}
package lk.ac.jfn.vau.MyDBapi.Repo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Repo {
private static final String DB_Driver = "com.mysql.jdbc.Driver";
private static final String DB_Connection = "jdbc:mysql://localhost:3306/";
private static final String DB_Name = "departmentapi";
private static final String DB_User = "root";
private static final String DB_Password = "";
public static void getDriver() {
try {
Class.forName(DB_Driver);
System.out.println("Driver found");
} catch (ClassNotFoundException e) {
System.out.println("Driver not found " + e.getMessage());
}
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(DB_Connection + DB_Name, DB_User, DB_Password);
System.out.println("Database Connected");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return connection;
}
public ResultSet get(String query, Connection connection) {
ResultSet resultSet = null;
try {
Statement statement = connection.createStatement();
resultSet = statement.executeQuery(query);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return resultSet;
}
}
package lk.ac.jfn.vau.MyDBapi.Repo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import lk.ac.jfn.vau.MyDBapi.Model.Department;
public class DepartmentRepo extends Repo {
Connection connection=null;
public DepartmentRepo() {
getDriver();
connection = getConnection();
}
Map<Long, Department>map=new HashMap<Long, Department>();
public Collection<Department> getAll(){
ResultSet resultSet=get("select * from department", connection);
try {
while(resultSet.next()) {
Department department=new Department(resultSet.getLong(1), resultSet.getString(2), resultSet.getString(3));
map.put(department.getId(), department);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return map.values();
}
}
package lk.ac.jfn.vau.MyDBapi;
import java.util.Collection;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import lk.ac.jfn.vau.MyDBapi.Model.Department;
import lk.ac.jfn.vau.MyDBapi.Repo.DepartmentRepo;
@Path("/dept")
@Produces(value = {MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Consumes(MediaType.APPLICATION_JSON)
public class DepartmentResource {
private DepartmentRepo repo=new DepartmentRepo();
@GET
public Response getDepartments() {
Collection<Department> departments = repo.getAll();
if(!departments.isEmpty()) {
return Response.status(Status.FOUND).entity(departments).build();
}
return Response.status(Status.NOT_FOUND).entity("Nothing Found").build();
}
}
POM.xml
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
【问题讨论】:
-
返回
new GenericEntity<Collection<Department>>(departments){}作为响应实体。如果您不这样做,JAXB 提供程序将无法推断类型。 JSON 作为提供者工作,只是使用自省。 -
谢谢,它有效。像演员表吗?
-
不,JAXB 需要知道类才能序列化。当您调用 getClass() 时,它将返回 HashMap.Values,而 JAXB 不知道如何使用它。当您使用 GenericEntity 时,提供者可以从匿名类中获取泛型参数。注意
new GE<Type>(){}末尾的括号。这是一个匿名类。如果你刚刚做了new GE<Type>(),它就行不通,因为由于类型擦除,你无法以这种方式获取泛型类型信息。所以你需要传递一个匿名类。 -
很好的解释,谢谢