相等类之间的ClassCastException Wildfly 10 [英] ClassCastException in between equal classes Wildfly 10
问题描述
我正在创建RESTful应用程序,但无法进行新连接的转换。
我的应用程序服务器是Wildfly 10.0。
I am Creating a RESTful application and I am having trouble making a conversion for a new connection. My application server is Wildfly 10.0.
standalone-full.xml中的数据源和驱动程序:
<datasource jndi-name="java:jboss/datasources/PostgreDS" pool-name="PostgreDS" enabled="true" use-java-context="true">
<connection-url>jdbc:postgresql://192.168.0.112:5432/bdns</connection-url>
<driver>postgresql</driver>
<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>40</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>sa</user-name>
<password>000000</password>
</security>
<statement>
<prepared-statement-cache-size>32</prepared-statement-cache-size>
<share-prepared-statements>true</share-prepared-statements>
</statement>
</datasource>
<drivers>
<driver name="postgresql" module="org.postgresql">
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>
</drivers>
Java代码ConnectionMng.java:
package dao;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.jboss.jca.adapters.jdbc.jdk7.WrappedConnectionJDK7;
import org.postgresql.jdbc.PgConnection;
public class ConnectionMng {
private DataSource ds;
private ConnectionMng() throws NamingException {
this.ds = (DataSource)new InitialContext().lookup("java:jboss/datasources/PostgreDS");
}
public PgConnection getPgConnection() throws SQLException {
WrappedConnectionJDK7 wrappedConn = (WrappedConnectionJDK7)ds.getConnection();
Connection underlyingConn = wrappedConn.getUnderlyingConnection();
return (PgConnection)underlyingConn;
}
}
发生异常:
ClassCastException: org.jboss.jca.adapters.jdbc.jdk7.WrappedConnectionJDK7 cannot be cast to org.jboss.jca.adapters.jdbc.jdk7.WrappedConnectionJDK7
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Core</groupId>
<artifactId>Core</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Core</name>
<description>Core</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<javac.target>1.8</javac.target>
<postgresql.enforce.jdk.version>[1.7,1.8)</postgresql.enforce.jdk.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-connector</artifactId>
<version>10.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.8</version>
</dependency>
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>0.7</version>
</dependency>
<dependency>
<groupId>org.zeroturnaround</groupId>
<artifactId>zt-zip</artifactId>
<version>1.9</version>
<type>jar</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
推荐答案
aribeiro的答案是正确的答案:一个由build-tool(Maven)打包的jar位于战争的lib目录中,并且与wildfly-Classloader冲突。
The answer from aribeiro is the right answer: A jar, packaged by the build-tool(Maven) is in the lib-directory of the war and conflicts with the wildfly-Classloader.
此外,以下问题的通用解决方案:
Additional,here is a generic solution for the following issue:
- 您已经拥有JBoss 7 AS或Wildfly AS
- 您使用Eclipse JBoss Tools
- 您部署了EAR或WAR,lib-Folder由Maven之类的构建工具打包
- 会出现这样的错误:examplepackage.ExcampleClass无法转换为examplepackage.ExcampleClass
- you've got a JBoss 7 AS or a Wildfly AS
- You work with Eclipse JBoss Tools
- you deploy an EAR or a WAR, lib-Folder is packed by build-tool like Maven
- there occurs an error like this: examplepackage.ExcampleClass cannot be cast to examplepackage.ExcampleClass
大多数情况是在您离开Java EE-Spec并使用特定于JBoss / Wildfly的模块,ironjacamar,jsf-impl等时发生的。 Maven将所需的jar放在lib目录中。但是Jars已经在AS的模块目录中。根据我的经验, ClassCastException
出于这个原因而发生。
Mostly it happens, when you leave the Java EE-Spec and use JBoss/Wildfly-specific modules, ironjacamar, jsf-impl and so on. Maven puts the required jars in the lib-directory. But the Jars are already in the module-directory of the AS. According to my experience, the ClassCastException
occurs for this reason.
然后必须从lib-中删除jar。
You must then remove the jar from the lib-directory, in maven by declaring the dependency as provided.
告诉JBoss / Wildfly-Classloader在哪里找到该类。可以通过以下方式完成:
Tell the JBoss/Wildfly-Classloader, where to find the class. This can be done by:
-
向
MANIFEST.MF $ c添加依赖项$ c>的项目。 Eclipse-JBoss-Tools从服务器环境中加载类,因此您可以从pom中删除依赖项。在您的情况下:
Either add a dependency to the
MANIFEST.MF
of the project. Eclipse-JBoss-Tools loads the class from the Server-Enviroment, so you can remove the dependency from the pom. In your case:
Dependencies: org.jboss.ironjacamar.jdbcadapters
或者您使用 jboss-deployment-structure.xml
,则可以加载该类与Maven,但声明为已提供
Or you use the jboss-deployment-structure.xml
, then you can load the class with Maven, but declare it as provided
<dependencies>
<module name="org.jboss.ironjacamar.jdbcadapters" />
</dependencies>
另请参见 https://docs.jboss.org/author/display/WFLY8/Class+Loading+in + WildFly
这篇关于相等类之间的ClassCastException Wildfly 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!