简体   繁体   English

找不到媒体类型 = 应用程序/xml,类型 = 类 java.util.HashMap$Values 的 MessageBodyWriter

[英]MessageBodyWriter not found for media type=application/xml, type=class java.util.HashMap$Values

My API method couldn't convert object to XML but it can converts to JSON.我的 API 方法无法将 object 转换为 XML 但它可以转换为 Z0ECD11C1D7A287401D148A2F when the method return type is Collection (MAP) it converts to XML without any runtime exceptions (MessageBodyWriter not found) but when use Response as return type it causes the runtime exceptions.当方法返回类型是 Collection (MAP) 时,它会转换为 XML 而没有任何运行时异常(未找到 MessageBodyWriter),但是当使用 Response 作为返回类型时,它会导致运行时异常。 here I mention the codes.这里我提到了代码。 I think I don't have any issues in my code but problems on dependencies.我认为我的代码中没有任何问题,但依赖项有问题。

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 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>

With JAXB (XML provider), you need to use GenericEntity .使用 JAXB(XML 提供程序),您需要使用GenericEntity

GenericEntity<Collection<Department>> entity
    = new GenericEntity<Collection<Department>>(departments){};
return Response.ok(entity).build();

The reason you need to do this is because JAXB needs to know the type information in order to serialize.您需要这样做的原因是因为 JAXB 需要知道类型信息才能进行序列化。 When you just return the generic entity, as opposed to Response , the generic type information is in the method signature当您只返回泛型实体时,与Response ,泛型类型信息位于方法签名中

@GET
public Collection<Department> getDepartments() {}

When you use Response , generic information is not available due to type erasure.当您使用Response时,由于类型擦除,一般信息不可用。 So what JAX-RS allows is to use GenericEntity .所以 JAX-RS 允许使用GenericEntity But you cannot just use an instance, because type erasure does not allow for this.但是你不能只使用一个实例,因为类型擦除不允许这样做。 You actually need to use an anonymous class您实际上需要使用匿名 class

new GenericEntity<Collection<Department>>(departments){}

Notice the curly braces at the end.注意最后的花括号。 This is an anonymous class.这是一个匿名的 class。 Through reflection, you are able to get generic type parameters from this anonymous class.通过反射,您可以从这个匿名的 class 中获取泛型类型参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未找到类型为java.util.HashMap的返回值的转换器 - No converter found for return value of type: class java.util.HashMap 找不到媒体类型=应用程序/json,类型=类 java.util.ArrayList 的 MessageBodyWriter - MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList 找不到 Media type=application/xml 的 MessageBodyWriter - MessageBodyWriter not found for media type=application/xml java.lang.IllegalArgumentException:未找到类型为java.util.HashMap的返回值的转换器 - java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.HashMap HttpMessageNotWritableException:未找到类型的返回值的转换器:class java.util.HashMap 在 Z381008EDD81C2 中 - HttpMessageNotWritableException: No converter found for return value of type: class java.util.HashMap in Spring MVC 未找到 Media type=text/plain、type=class java.util.ArrayList、genericType=java.util.List 的 MessageBodyWriter<models.Person> - MessageBodyWriter not found for media type=text/plain, type=class java.util.ArrayList, genericType=java.util.List<models.Person> 找不到Java类型类java.util.ArrayList和MIME媒体类型application / xml的消息正文编写器 - A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found MessageBodyProviderNotFoundException:找不到针对媒体type = application / json,type = class java.lang.Long的MessageBodyWriter - MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.lang.Long 严重收到错误:找不到媒体类型= application / xml的MessageBodyWriter, - Getting error SEVERE: MessageBodyWriter not found for media type=application/xml, 泽西岛+灰熊HTTP +杰克逊(未针对媒体类型= application / xml找到MessageBodyWriter) - Jersey + Grizzly HTTP + Jackson (MessageBodyWriter not found for media type=application/xml)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM