gpt4 book ai didi

java - spring boot 中嵌入的geoserver 与hibernate/jpa 冲突

转载 作者:行者123 更新时间:2023-12-01 14:34:12 30 4
gpt4 key购买 nike

我正在尝试使用 spring boot web 创建一个 web 应用程序,它需要 geoserver 来配得上 GeoJSON 层。

我成功地在与我的 spring-boot 应用程序相同的 tomcat 中启动了地理服务器。但是当 GeoServer 试图访问 postgresql/postgis 数据库时出现问题。地理服务器说:

Error occurred getting featuresUnable to obtain connection: Cannot create PoolableConnectionFactory

由于:

java.lang.ClassCastException: class org.postgis.PGbox2d
at java.lang.Class.asSubclass(Class.java:3404) ~[na:1.8.0_151]
at org.postgresql.jdbc.PgConnection.initObjectTypes(PgConnection.java:645) ~[postgresql-42.1.1.jar:42.1.1]
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:296) ~[postgresql-42.1.1.jar:42.1.1]
at org.postgresql.Driver.makeConnection(Driver.java:450) ~[postgresql-42.1.1.jar:42.1.1]
at org.postgresql.Driver.connect(Driver.java:252) ~[postgresql-42.1.1.jar:42.1.1]
...

我可以通过 spring 应用程序访问存储在 postgresql/postgis 中的数据。

这是我的pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>net.postgis</groupId>
<artifactId>postgis-jdbc</artifactId>
<version>2.2.0</version>
<exclusions>
<exclusion>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>com.bedatadriven</groupId>
<artifactId>jackson-datatype-jts</artifactId>
<version>2.2</version>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.2</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

还有应用:

package fr.app;

import fr.app.dao.GreffeRepository;
import fr.app.util.TomcatDeployer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;

import java.util.logging.Logger;

@SpringBootApplication
public class GroLocApplication
{
private static final Logger LOGGER = Logger.getLogger(GroLocApplication.class.getName());

public static void main(String... args)
{
SpringApplication.run(GroLocApplication.class, args);
}

@Bean
public EmbeddedServletContainerFactory servletContainerFactory()
{
return new TomcatDeployer();
}
}

TomcatDeployer,在资源中的 war 文件夹中部署 war:

package fr.app.util;

import org.apache.catalina.Context;
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.startup.Tomcat;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.logging.Logger;

final public class TomcatDeployer extends TomcatEmbeddedServletContainerFactory
{
private static final Logger LOGGER = Logger.getLogger(TomcatDeployer.class.getName());
private static final String WAR_TO_DEPLOY = "war/geoserver.war";

private String copyResourceToTmp() throws IllegalStateException
{
try (InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(WAR_TO_DEPLOY))
{
File tmpWar = File.createTempFile("geoserver", ".war");
Files.copy(resourceStream, tmpWar.toPath(), StandardCopyOption.REPLACE_EXISTING);
return tmpWar.getAbsolutePath();
}
catch (IOException e)
{
throw new IllegalStateException("Cannot copy geoserver.war resource", e);
}
}

@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat)
throws IllegalStateException
{
if (!new File(tomcat.getServer().getCatalinaBase(), "webapps").mkdirs())
{
throw new IllegalStateException("Cannot create a webapps directory on tomcat");
}

try
{
final String pathToWar = copyResourceToTmp();
LOGGER.info(pathToWar);
final Context context = tomcat.addWebapp("/geoserver", pathToWar);
final WebappLoader loader = new WebappLoader(Thread.currentThread().getContextClassLoader());
context.setLoader(loader);
}
catch (ServletException e)
{
throw new IllegalStateException("Failed to add geoserver", e);
}
return super.getTomcatEmbeddedServletContainer(tomcat);
}
}

Greffe 实体:

package fr.app.entity;

import com.bedatadriven.jackson.datatype.jts.serialization.GeometryDeserializer;
import com.bedatadriven.jackson.datatype.jts.serialization.GeometrySerializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.vividsolutions.jts.geom.Geometry;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Greffe
{
@Id
private String id;

@JsonSerialize(using = GeometrySerializer.class)
@JsonDeserialize(using = GeometryDeserializer.class)
private Geometry coord;
// getters...
}

而dao是经典CrudRepository<Greffe,String>延伸。

那么有没有可能真的把GeoServer和spring-boot应用的tomcat嵌入到同一个tomcat中呢? (这是可能的,因为我这样做了,但这真的是个好主意吗?)

如果是这样,我该如何避免这种麻烦呢?!

提前致谢:)

最佳答案

我不会将 GeoServer 嵌入到您的应用程序正在使用的同一个 Tomcat 中。是的,有可能,是的;但不是一个好主意。推理:在 Tomcat 启动期间,GeoServer 和您的应用程序(尚未)未运行 - 但如果您的应用程序在启动期间需要 GeoServer,则它必须失败。解决方案:将 GeoServer 放在一个 serparat Tomcat 中,该 Tomcat 在您的应用程序启动之前启动。

麻烦你了(还有 ClassCastException):确保使用彼此匹配的 postgres 和 postgis 驱动程序版本以及您正在使用的数据库版本。您得到的错误似乎与 https://github.com/brettwooldridge/HikariCP/issues/1476 有关- 也许那里的信息可以帮助您。

我个人从未听说过 com.bedatadriven.jackson.datatype.jts - 确保该库的依赖 JTS 版本与您的 GeoServer 中使用的版本匹配。

当您使用 HibernateSpatial 时:确保正确配置 HibernateSpatial。我没有看到配置。也许将 hibernate-spatial 用于 postgis 是一种解决方案:

<groupId>org.hibernatespatial</groupId>
<artifactId>hibernate-spatial-postgis</artifactId>

关于java - spring boot 中嵌入的geoserver 与hibernate/jpa 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47894557/

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