使用Wildfly插入Postgres的json列 [英] Inserting Postgres' json column using wildfly

查看:110
本文介绍了使用Wildfly插入Postgres的json列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个Web应用程序,它是一个剩余的API,并且正在使用wildfly 9.0.2和PostgreSQL。数据库连接使用的是最新的jdbc4.2驱动程序,而jre是oracle的jr8。

I'm running a web application which is a rest API and I'm using wildfly 9.0.2 and PostgreSQL. The database connection is using the latest jdbc4.2 driver and the jre is oracle's jr8.

我是在主题,到目前为止,它运行得很好,我已经安装了

I installed the jdbc following this topic, and got it running perfectly so far I installed the datasource using the wildfly admin console.

因此,当我尝试插入json字段时,问题就到了。我很快意识到JDBC没有直接的方法来插入json字段,比如说String,Integer,Timestamp等。所以我在网上现在不记得此解决方案了:

So the issue arrived when I attempted to insert a json field. I quickly realized JDBC doesn't have a direct method to insert a json field as it has for let's say String, Integer, Timestamp etc. So I find this solution online somewhere I don't remember now:

oPGobject jsonObject = new PGobject();
jsonObject.setType("json");
jsonObject.setValue(new Gson().toJson(ctrlClienteComunicacao));
preparedStatement.setObject(++i,jsonObject);

当我尝试从一个简单的主类运行它时,此解决方案效果很好,我可以插入任何json想要,但是当我在wildfly上运行时尝试这段代码时,却找不到此类错误:

This solution works fine as I tried it running from a simple main class and I can insert any json I want, but when I tried this code while running on wildfly I got this class not found error:

java.lang.NoClassDefFoundError: org/postgresql/util/PGobject

我的第一个尝试是从JDBC库中删除提供的属性,因此它将包含在.war中。然后我收到一个新错误:

My first atempt was to remove the provided attribute from the JDBC library so it would be included it in the .war. And then I got a new error:

org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.postgresql.util.PGobject. Use setObject() with an explicit Types value to specify the type to use.

经过几次尝试,我意识到在战争中加入jar会使wildfly将其部署为驱动程序。

After a few more attempts I realized that including the jar in the war makes wildfly deploy it as a driver.

[org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) WFLYJCA0018: Started Driver service with driver-name = VirtualForum.war_org.postgresql.Driver_9_4

所以我尝试了另一件事,而部署了战争,我将数据源从原始pgsql驱动程序更改为在应用程序部署期间启动的数据源,它有效!我可以插入json字段。

So I tried a different thing, while the war was deployed, I changed the datasource from the original pgsql driver to this one started during deployment of the app and it works! I can insert json fields.

但是我认为这不是一个合适的解决方案,因为我更喜欢使用安装在wildfly上的驱动程序。

But I don't think this is a proper solution as I would prefer it using the driver installed on wildfly.

我对我的英语和问题深表歉意。布局,这是我的第一个问题。

I apologize for my english and the question layout this is my very first question.

推荐答案

添加 org.postgres 依靠战争宣言为我解决了这个问题。

Adding org.postgres dependency to the war manifest solved the issue for me.

这里是 Maven 片段:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Dependencies>org.postgres</Dependencies>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

这是生成的 MANIFEST.MF (请参阅最后一行):

And here is the generated MANIFEST.MF (see the last line):

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: dedek
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_51
Dependencies: org.postgres

这篇关于使用Wildfly插入Postgres的json列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆