GSON 转换抛出 "failed making field [package] accessible" 错误

GSON conversion throws "failed making field [package] accessible" error

我有一个 Servlet 代码,我 运行 从 MySQL 查询数据并使用 gson 将其转换为 json 对象。最终我想在我的 React 应用程序中使用 axios 在前端显示它。不过,我总是 运行 遇到 GSON 的问题,而且它永远无法正确编译。之前是 2.8 版本,所以我升级到 2.9 以消除反射错误,但现在我得到了这个错误。

com.google.gson.JsonIOException: Failed making field 'java.lang.ref.Reference#referent' accessible; either change its visibility or write a custom TypeAdapter for its declaring type

我的servlet程序代码:


package org.datafetching;

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.sql.*;

import com.google.gson.Gson;

@WebServlet("/fetchdata")
public class FetchData extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    public FetchData() {
    }
    
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/grey_goose", "", "");
            PreparedStatement ps=conn.prepareStatement("select * from table limit 10");
            ResultSet rs=ps.executeQuery();
            
            Gson gson = new Gson();
            String tablejson = gson.toJson(rs);
            
            PrintWriter printWriter = response.getWriter();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            printWriter.print(tablejson);
            printWriter.flush();

            //printWriter.write(tablejson);
            
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

}

对象class代码

package servdemo.registration.model;

public class Employee {

    private String firstName;
    private String lastName;
    private String username;
    private String password;
    private String address;
    private String contact;
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getContact() {
        return contact;
    }
    public void setContact(String contact) {
        this.contact = contact;
    }
}


您应该将结果集映射到代表 table 的对象。例如,如果您 table 命名为 People 并且它有字段名称和年龄。

示例class

public Class People {
  private String name;
  private Integer age;
  ... //Getters and Setters here
}

这里是映射数据的代码。

List<People> staffs = new ArrayList<People>();
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
  People people = new People(resultSet.getString("name"),
                             resultSet.getInt("age"));
  staffs.add(people);
}
resultSet.close();
preparedStatement.close();
connection.close();
Gson gson = new Gson();

String yourJson = gson.toJson(staffs);

此代码将映射您的结果集。当然,你需要根据你的 table 创建对象,所以在稍微更改此代码后你应该可以让它工作。