日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了找不到媒體type=application/xml、type=class java.util.HashMap$Values的MessageBodyWriter的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

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

推薦答案

對于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。但是您不能只使用實例,因為類型擦除不允許這樣做。您實際上需要使用匿名類

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

注意末尾的花括號。這是一個匿名類。通過反射,您可以從此匿名類獲取泛型類型參數。

這篇關于找不到媒體type=application/xml、type=class java.util.HashMap$Values的MessageBodyWriter的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:application Class HashMap type xml 媒體 找不到
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定