嵌入在spring boot和hibernate / jpa冲突中的geoserver

时间:2017-12-19 20:16:18

标签: java spring-boot geoserver

我正在尝试使用spring boot web创建一个Web应用程序,需要geoserver才能获得GeoJSON图层。

我成功地在我的spring-boot应用程序的同一个tomcat中启动了geoserver。但是当GeoServer尝试访问postgresql / postgis数据库时出现问题。 Geoserver说:

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中吗? (这是可能的,因为我这样做了,但这真的是一个好主意吗?)

如果是这样,我怎么能避免这种麻烦?!

感谢提前:)

1 个答案:

答案 0 :(得分:1)

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