English 中文(简体)
com.fasterxml.jackson.databind.exc.InvalidDefinitionException类型`com.app.bankapplet.bank的类型定义无效`
原标题:com.fasterxml.jackson.databind.exc.InvalidDefinitionException Invalid type definition for type `com.app.bankapplet.bank.Bank`

尝试创建Java对象并将其存储在NOsql对象(NO2又称亚硝酸盐)数据库

在尝试将对象存储在对象存储库中时,会引发com.fasterxml.jackson.databind.exc.InvalidDefinitionException异常。

我试图在fo.java模块文件中导出bank.java类;我试图在bank.java类中添加一个默认构造函数。两者都没有解决问题。

感谢您帮助解决此错误。

完整错误显示如下:

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid type definition for type `com.app.bankapplet.bank.Bank`: Failed to construct BeanSerializer for [simple type, class com.app.bankapplet.bank.Bank]: (java.lang.IllegalArgumentException) Failed to call `setAccess()` on Field  id  (of class `com.app.bankapplet.bank.Bank`) due to `java.lang.reflect.InaccessibleObjectException`, problem: Unable to make field private int com.app.bankapplet.bank.Bank.id accessible: module com.app.bankapplet does not "opens com.app.bankapplet.bank" to unnamed module @6293abcc
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:72)
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadTypeDefinition(SerializerProvider.java:1280)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.constructBeanOrAddOnSerializer(BeanSerializerFactory.java:475)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.findBeanOrAddOnSerializer(BeanSerializerFactory.java:295)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory._createSerializer2(BeanSerializerFactory.java:240)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:174)
    at com.fasterxml.jackson.databind.SerializerProvider._createUntypedSerializer(SerializerProvider.java:1503)
    at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:1451)
    at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:556)
    at com.fasterxml.jackson.databind.SerializerProvider.findTypedValueSerializer(SerializerProvider.java:834)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:307)
    at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4522)
    ... 18 more
Exception running application com.app.bankapplet.Main

我所期望的是对象存储在对象存储库中。

请在下面找到我的代码:

主类

package com.app.bankapplet;

import com.app.bankapplet.bank.Bank;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.dizitart.no2.Nitrite;
import org.dizitart.no2.NitriteCollection;
import org.dizitart.no2.objects.ObjectRepository;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

public class Main extends Application {
    private static Nitrite db;
    private static Bank bank;
    @Override
    public void start(Stage stage) throws IOException {
        // Generates a bank
        bank = getBank();
        ObjectRepository<Bank> bankObjectStore = db.getRepository(Bank.class);
        bankObjectStore.insert(bank);

        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("main.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 800, 600);
        stage.setTitle("Bank Manager");
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        db = Nitrite.builder()
                .filePath(new File("target/appdb.db"))
                .compressed()
                .openOrCreate();
        launch();
    }
    protected static Bank getBank() {
        Dialog<Bank> dialog = new Dialog<>();
        dialog.setTitle("New Bank");
        dialog.setHeaderText("Enter the name of the bank:");

        Label label = new Label("Bank Name: ");
        TextField text = new TextField();

        GridPane gridPane = new GridPane();
        gridPane.add(label, 1, 1);
        gridPane.add(text, 2, 1);
        dialog.getDialogPane().setContent(gridPane);

        ButtonType buttonTypeOK = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().add(buttonTypeOK);

        dialog.setResultConverter(new Callback<ButtonType, Bank>() {
            @Override
            public Bank call(ButtonType b) {
                if(b == buttonTypeOK) {
                    return new Bank(text.getText());
                }
                return null;
            }
        });

        Optional<Bank> result = dialog.showAndWait();

        if(result.isPresent()) {
            return result.get();
        }

        return null;
    }
}

银行.java

package com.app.bankapplet.bank;

import org.dizitart.no2.objects.Id;

public class Bank {
    @Id
    private int id;
    private String name;

    public Bank() {

    }
    public Bank(String name) {
        this.id = 0;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private static String toTitleCase(String item) {
        if (item == null || item.isEmpty()) {
            return item;
        }

        StringBuilder convertedString = new StringBuilder();
        boolean convertNext = true;

        for (char ch : item.toCharArray()) {
            if (Character.isSpaceChar(ch)) {
                convertNext = true;
            } else if (convertNext) {
                ch = Character.toTitleCase(ch);
                convertNext = false;
            } else {
                ch = Character.toLowerCase(ch);
            }
            convertedString.append(ch);
        }

        return convertedString.toString();
    }
}

fo.java模块

module com.app.bankapplet {
    requires javafx.controls;
    requires javafx.fxml;
    requires nitrite;

    opens com.app.bankapplet to javafx.fxml;
    exports com.app.bankapplet.bank;
    exports com.app.bankapplet;
}

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.app</groupId>
    <artifactId>BankApplet</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>BankApplet</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.9.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>20</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>20</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.1</version>
        </dependency>
        <dependency>
            <groupId>org.dizitart</groupId>
            <artifactId>nitrite</artifactId>
            <version>3.4.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>20</source>
                    <target>20</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.app.bankapplet/com.app.bankapplet.Main</mainClass>
                            <launcher>app</launcher>
                            <jlinkZipName>app</jlinkZipName>
                            <jlinkImageName>app</jlinkImageName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
问题回答

java.lang.reflect.InaccessableObjectException,问题:无法使字段私有int com.app.bankapplet.bank.id可访问

为id属性添加setter,以便jackson可以访问它

public void setId(int id) {
   this.id= id;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签