Quarkus集成PostGIS遇两类错误,求解决方案
PostGIS + Quarkus 集成问题排查
问题1:启动阶段的NoSuchMethodError
运行PostGIS简单示例时触发以下错误:
Caused by: java.lang.NoSuchMethodError: 'void org.hibernate.dialect.PostgreSQL81Dialect.registerColumnType(int, java.lang.String)'
对应环境与配置
- Quarkus Dev Service 启动的PostgreSQL版本:14.8
pom.xml依赖片段:
... <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-hibernate-orm-panache</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-spatial</artifactId> <version>5.5.5.Final</version> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-jdbc-postgresql</artifactId> </dependency> <dependency> <groupId>net.postgis</groupId> <artifactId>postgis-jdbc</artifactId> <version>2021.1.0</version> </dependency> ...
application.properties配置:
quarkus.hibernate-orm.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
问题2:接口调用阶段的字节序错误
改用PostGIS Docker镜像自建数据库后,服务启动正常,但调用POST接口时触发:
Caused by: org.postgresql.util.PSQLException: ERROR: Invalid endian flag value encountered.
自建数据库相关信息
- 尝试过的Docker镜像:
- postgis/postgis:15-3.3(PostgreSQL15.3+PostGIS3.3.4)
- postgis/postgis:14-3.3(PostgreSQL14.8+PostGIS3.3.4)
- postgis/postgis:9.6-3.2(PostgreSQL9.6.24+PostGIS3.2.0,使用此版本会回到第一个方法不存在错误)
application.properties(dev环境配置):
%dev.quarkus.datasource.db-kind=postgresql %dev.quarkus.datasource.username=postgres %dev.quarkus.datasource.password=mysecretpassword %dev.quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/test %dev.quarkus.hibernate-orm.database.generation=drop-and-create quarkus.hibernate-orm.dialect=org.hibernate.dialect.PostgreSQLDialect
核心代码片段
实体类Route.java
@Entity public class Route implements Serializable { @Id @GeneratedValue(strategy = GenerationType.UUID) private UUID id; @Column(nullable = false) private String description; @JsonIgnore @Column(columnDefinition = "Geometry") private PGgeometry coordinates; // 省略其他构造、getter/setter代码 }
请求DTORouteDTO.java
public class RouteDTO { public String description; public double[][] coordinates; }
接口类RouteResource.java
@Path("/routes") public class RouteResource { @Inject RouteRepository repository; private static PGgeometry createPGGeometry(double[][] coordinates) { Point[] points = new Point[coordinates.length]; for (int i = 0; i < coordinates.length; i++) { if (coordinates[i].length != 2) { throw new IllegalArgumentException("Invalid coordinate format: " + coordinates[i]); } points[i] = new Point(coordinates[i][0], coordinates[i][1]); } LinearRing outerRing = new LinearRing(points); return new PGgeometry(outerRing); } @POST @Consumes(MediaType.APPLICATION_JSON) public Response create(RouteDTO routeDTO) { Route route = new Route(routeDTO.description, createPGGeometry(routeDTO.coordinates)); repository.persist(route); return Response.created(URI.create("/routes/" + route.getId())).build(); } }
请求体示例
{ "description": "Route 66", "coordinates": [ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0] ] }
疑问
如何解决上述两个错误,实现PostGIS与Quarkus的正常集成?
内容的提问来源于stack exchange,提问作者Luciano Borges
相关产品推荐
相关产品推荐

