无法强制转换为类-他们在未命名的模块加载程序’应用程序’

我试图从由 wsdl2java生成的源代码中创建一个 bean。

每次我尝试运行 Spring Boot 应用程序时,都会得到以下错误:

由 java.lang. ClassCastException: class 引发 无法将 ClientImpl 强制转换为类 Xignite.services. XigniteCurrenciesSoap (org.apache.cxf.endpoint. ClientImpl 和 Xignite.services. XigniteCurrenciesSoap 的未命名模块为 应用程式)

我不确定如何将生成的源代码作为模块包含在我的主 SpringBoot 应用程序中。

我的目录结构是:

├── build
│   └── generatedsources
│       └── src
│           └── main
│               └── java
│                   └── com
│                       └── xignite
│                           └── services
│
└── src
└── main
├── java
│   └── io
│       └── mateo
│           └── stackoverflow
│               └── soapconsumption
└── resources
       └── wsdls

相关系统信息:

openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)
  • Spring Boot 2.1.2. RELEASE
  • 5.2级

我还把这个项目上传到了 Github: https://github.com/ciscoo/soap-consumption-spring-boot

244286 次浏览

I had a similar case, and (as mentioned by @Holger in the comment) the module info in the message is simply misleading - this is an actual case of trying to cast something to something that doesn't match it.

In your case, ClientImpl simply is not a subtype of XigniteCurrenciesSoap.

The stacktrace is trying to tell you that you have casted XigniteCurrenciesSoap to ClientImpl.

Such as the example below:

Object returnObj= getXigniteCurrenciesSoap();
return (ClientImpl) returnObj;

You have to find out where you did that in your code and fix it.

I had the same problem. The problem in my case was that I already had a class with the same name in another place. So try changing the class name.

I was getting very similar above exception.

java.lang.ClassCastException: class [B cannot be cast to class org.apache.avro.generic.GenericRecord ([B is in module java.base of loader 'bootstrap'; org.apache.avro.generic.GenericRecord is in unnamed module of loader org.springframework.boot.loader.LaunchedURLClassLoader

Root cause was my Gradle dependency had exclude statement, i.e.

exclude group: 'org.springframework.cloud', module: 'spring-cloud-stram-binder-kafka',

I commented it as below and things were working fine after that:

implementation ('com.xyz.lux:lux-acitor:1.25.0') {
//exclude group: 'org.springframework.cloud', module: 'spring-cloud-stream-binder-kafka'
exclude group: 'org.slf4j', module: 'slf4j-reload4j'
exclude group: 'io.confluent', module: 'confluent-log4j'
}