I tried to create a simple application with a geometry persistent layer (Spring boot with hibernate-spatial)
Here is my Entity class :
// Annotations from lombok project
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@Entity
@Table(name = "persistent_geometry")
public class PersistentGeometry implements Serializable {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "id", columnDefinition = "VARCHAR(255)")
private UUID id;
@JsonSerialize(using = GeometrySerializer.class)
@JsonDeserialize(using = GeometryDeserializer.class)
@Column(name = "geometry", columnDefinition = "geometry(Polygon,4326)")
private Polygon geometry;
}
Here is my repository interface
public interface PersistentGeometryService extends CrudRepository<PersistentGeometry, UUID> {}
Here is my controller class
@RestController
public class PersistenGeometryController {
@Autowired
private PersistentGeometryService persistentGeometryService;
@PostMapping(value="/persistentGeometry")
public ResponseEntity createNewGeom(@RequestBody PersistentGeometry persistentGeometry) {
PersistentGeometry newGeom = persistentGeometryService.save(persistentGeometry);
var headers = new HttpHeaders();
headers.add("Location", "/persistentGeometry/" + newGeom.getId().toString() );
return new ResponseEntity(headers, HttpStatus.CREATED);
}
}
However, when i do a POST request with this payload :
{
"geometry" : "POLYGON((<Some Coordinates here>))"
}
Or its GeoJSON version :
{
"geometry" : {"type":"Polygon","coordinates":[[[<Some Coordinates here>]]]}
}
I got this error from my spring app (com.geomdemo.peristentgeometry is my base package name) :
Failed to evaluate Jackson deserialization for type [[simple type, class com.geomdemo.peristentgeometry.model.PersistentGeometry]]:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.bedatadriven.jackson.datatype.jts.serialization.GeometryDeserializer': Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bedatadriven.jackson.datatype.jts.parsers.GeometryParser' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Failed to evaluate Jackson deserialization for type [[simple type, class com.geomdemo.peristentgeometry.model.PersistentGeometry]]:
com.fasterxml.jackson.databind.JsonMappingException: Class com.bedatadriven.jackson.datatype.jts.serialization.GeometryDeserializer has no default (no arg) constructor
I found a suggestion here to add a @Bean
of type JtsModule
so I did this :
@Configuration
public class JacksonConfig {
@Bean
public JtsModule jtsModule() {
return new JtsModule();
}
}
But it didn't help in my case....It seems clear that SPring boot is looking for a valid Beans of type GeometryParser...But How to implement such a Bean...did not found any documentation or example on how to perform this.