0

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.

jossefaz
  • 3,312
  • 4
  • 17
  • 40

1 Answers1

0

I was having the same problem with a deserializer, I was missing the default no arguments constructor after I added that constructor, everything worked fine.

I see that you have the Lombok annotations, I tried with those as well, but I was getting compilation errors, but I think is because my class is extends StdDeserializer.

cunhaf
  • 1,057
  • 1
  • 8
  • 12
  • hi and thank you for your answer. I did not understand the second part of it : what is the link between Lombok `@NoArgsConstructor` and the `extends StdDeserializer` what do you exactly suggest here ? – jossefaz Jun 26 '20 at 06:06
  • my suggestion would be to change the Lombok annotations and write the constructors by hand, to see if it solves the problem – cunhaf Jun 26 '20 at 10:03