![](/img/trans.png)
[英]No converter found for return value of type: class java.util.HashMap
[英]MessageBodyWriter not found for media type=application/xml, type=class java.util.HashMap$Values
我的 API 方法無法將 object 轉換為 XML 但它可以轉換為 Z0ECD11C1D7A287401D148A2F 當方法返回類型是 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>
使用 JAXB(XML 提供程序),您需要使用GenericEntity
。
GenericEntity<Collection<Department>> entity
= new GenericEntity<Collection<Department>>(departments){};
return Response.ok(entity).build();
您需要這樣做的原因是因為 JAXB 需要知道類型信息才能進行序列化。 當您只返回泛型實體時,與Response
,泛型類型信息位於方法簽名中
@GET
public Collection<Department> getDepartments() {}
當您使用Response
時,由於類型擦除,一般信息不可用。 所以 JAX-RS 允許使用GenericEntity
。 但是你不能只使用一個實例,因為類型擦除不允許這樣做。 您實際上需要使用匿名 class
new GenericEntity<Collection<Department>>(departments){}
注意最后的花括號。 這是一個匿名的 class。 通過反射,您可以從這個匿名的 class 中獲取泛型類型參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.