gpt4 book ai didi

java - ClassCastException 在相等的类 Wildfly 10 之间

转载 作者:行者123 更新时间:2023-11-30 08:38:19 24 4
gpt4 key购买 nike

我正在创建 RESTful 应用程序,但在为新连接进行转换时遇到问题。我的应用服务器是 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>

最佳答案

很明显,如前所述,由于依赖项冲突,您遇到了类加载器问题。

要解决它,您应该将您的wildfly-connector 依赖项标记为provided

<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-connector</artifactId>
<version>10.0.0.Final</version>
<scope>provided</scope>
</dependency>

WildFly 已包含 JAR。如果您转到以下文件夹,您可以找到它:

<wildfly-10.0.0.Final-dir>\modules\system\layers\base\org\jboss\as\connector\main

您无需将它与您的应用程序一起打包。

然而,如 WildFly 10's Implicit module dependencies for deployments 所述,需要显式触发 JCA 子系统。为此,可以使用具有以下内容的 jboss-deployment-structure.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="org.jboss.ironjacamar.jdbcadapters" slot="main"/>
</dependencies>
</deployment>
</jboss-deployment-structure>

然后,将您的 jboss-deployment-structure.xml 添加到您的 WAR 的 WEB-INF 文件夹中。

关于java - ClassCastException 在相等的类 Wildfly 10 之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36611894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com