Eclipse Maven XStream -无法使字段可访问-模块未向模块xstream“打开modele”

8wtpewkr  于 2022-11-22  发布在  Maven
关注(0)|答案(1)|浏览(221)

我不能使XStream工作,我不知道为什么。我在一个Maven项目,JRE-11,MVC模型,XStream 1. 4. 18。我是法国人顺便说一句。提前感谢。我只是把一个例子与UE类,但我想这样做与“Classe”和“Creneau”。我尝试了“xstream.alias”,但没有工作。

pom.xml

<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>Archi</groupId>
  <artifactId>Archi</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>javax.xml</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.18</version>
    </dependency>
  </dependencies>
</project>

Archi.java

package controlleur;

import com.thoughtworks.xstream.XStream;

import modele.*;
import vue.*;

public class Archi {
    public static void main(String[] args) {
        Classe classe = new Classe("IATIC5", 2020, 2021);
        UE ue = new UE("ARC", "EU-Archi");
        Creneau creneau = new Creneau(9, 11, 2021, 20, 22);
        
        Fenetre fenetre = new Fenetre();
        
        XStream xstream = new XStream();
        String xml = xstream.toXML(ue);
        System.out.println(xml);
        
        Controlleur controlleur = new Controlleur(classe, ue, creneau, fenetre);
    }
}

Controller.java

package controlleur;

import modele.*;
import vue.*;

public class Controlleur {  
    public Controlleur(Classe classe, UE ue, Creneau creneau, Fenetre fenetre) {
        fenetre.affiche(classe, ue, creneau);
    }
}

UE.java

package modele;

import java.util.UUID;

public class UE {
    private String id = UUID.randomUUID().toString(), sigle, nomination;

    public UE(String sigle, String nomination) {
        this.sigle = sigle;
        this.nomination = nomination;
    }

    //All getter, setter and toString
}

错误文本

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: No converter available
---- Debugging information ----
message             : No converter available
type                : modele.UE
converter           : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
message[1]          : Unable to make field private java.lang.String modele.UE.id accessible: module archi does not "opens modele" to module xstream
-------------------------------
    at xstream@1.4.18/com.thoughtworks.xstream.core.DefaultConverterLookup.lookupConverterForType(DefaultConverterLookup.java:88)
    at xstream@1.4.18/com.thoughtworks.xstream.XStream$1.lookupConverterForType(XStream.java:472)
    at xstream@1.4.18/com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:48)
    at xstream@1.4.18/com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:43)
    at xstream@1.4.18/com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:82)
    at xstream@1.4.18/com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.marshal(AbstractTreeMarshallingStrategy.java:37)
    at xstream@1.4.18/com.thoughtworks.xstream.XStream.marshal(XStream.java:1243)
    at xstream@1.4.18/com.thoughtworks.xstream.XStream.marshal(XStream.java:1232)
    at xstream@1.4.18/com.thoughtworks.xstream.XStream.toXML(XStream.java:1205)
    at xstream@1.4.18/com.thoughtworks.xstream.XStream.toXML(XStream.java:1192)
    at archi/controlleur.Archi.main(Archi.java:17)
axr492tv

axr492tv1#

我有同样的问题,我发现我的项目出了什么问题。
我正在用XStream开发一个在XML中具有数据持久性的JavaFX。当试图读取文件时,我遇到了同样的问题。
为了解决这个问题,我做了以下工作:

  • 将模型/javabeans保存在目录中。
MODULE/javabeans/
  PeopleXML.java
  PersonXML.java
  • 修改模块以允许XStream使用这些类(module-info.java
module MODULE {
    ...
    requires xstream;

    ...
    opens MODULE.javabeans to xstream;
    exports MODULE.javabeans;
}
  • 正常执行代码。
XStream xstream = new XStream();

xstream.addPermission(NoTypePermission.NONE);
xstream.addPermission(NullPermission.NULL);
xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);

xstream.allowTypes(new Class[]{PersonXML.class, PeopleXML.class});

xstream.allowTypesByWildcard(new String[] {
    "MODULE.javabeans.**"
});

xstream.alias("people", PeopleXML.class);
xstream.alias("person", PersonXML.class);

xstream.addImplicitCollection(PeopleXML.class, "lstPeople"); // Collection name

try {
    FileInputStream fis = new FileInputStream(xmlFilename);
        return (PeopleXML) xstream.fromXML(fis);
}
catch (FileNotFoundException e) {
    throw new InvalidDataException("File not found: " + xmlFilename);
}

注意:MODULE是您正在使用的模块。

相关问题