Kruschenstein
Kruschenstein

Reputation: 293

geoserver embedded in spring boot and hibernate/jpa conflict

I am trying to create a web application with spring boot web that needs geoserver to deserve GeoJSON layers.

I succed to launch geoserver in the same tomcat that my spring-boot application. But there is a problem when GeoServer is trying to access postgresql/postgis database. Geoserver said:

Error occurred getting featuresUnable to obtain connection: Cannot create PoolableConnectionFactory

Due to:

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]
...

I can access to data stored in postgresql/postgis via spring application.

Here there is my 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>

There is also Application:

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();
    }
}

The TomcatDeployer, that deploy war in war folder in resources:

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 entity:

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...
}

And dao is a classic CrudRepository<Greffe,String> extends.

So is it possible to really embed GeoServer in the same tomcat that the spring-boot application's tomcat? (it's possible because I did so, but is it really a good idea?)

And if so, how can I avoid this kind of trouble?!

Thanks by advance :)

Upvotes: 3

Views: 1892

Answers (1)

bkiselka
bkiselka

Reputation: 56

I would not embed GeoServer in the same Tomcat your application is using. Yes, it is possible, yes; but not a really good idea. Reasoning: During Tomcat startup, GeoServer and your application is not running (yet) - but if your application needs GeoServer during startup it must fail. Solution: Put GeoServer in a serparat Tomcat that starts up before your application is starting.

And to your trouble (and the ClassCastException): Make sure to use a postgres and postgis driver version matching each other and the database version you are using. The error you get seems related to https://github.com/brettwooldridge/HikariCP/issues/1476 - maybe the information there can help you.

I personally never heard of com.bedatadriven.jackson.datatype.jts - make sure the depending JTS version of that library matches the versions used in your GeoServer.

And as you are using HibernateSpatial: make sure to configure HibernateSpatial correct. I did not see a configuration. Maybe using hibernate-spatial for postgis is a solution:

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

Upvotes: 1

Related Questions