Automatic conversion of spatial field (Geometry) and geojson under Spring Boot (MVC)

In the previous article, using hibernate-spatial to enable Spring Data JPA to support spatial data , we used hibernate spatialand spring data jpasuccessfully supported the mapping and addition, deletion and modification of spatial fields. But the problem we are currently facing is how to convert the json data sent by the client into Geometry (Point, LineString), and how to directly convert the Geometry in the background to JSON. In the GIS world there is a standard GIS JSON format called geojson. This article will use the geojsonformat to convert to and from Geometry objects. Here our idea is that Spring Boot automatically registers for us MappingJackson2HttpMessageConverter, org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfigurationsuch as:

@Bean
@ConditionalOnMissingBean(value = MappingJackson2HttpMessageConverter.class, ignoredType = {
        "org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter",
        "org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter" })
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(
        ObjectMapper objectMapper) {
    return new MappingJackson2HttpMessageConverter(objectMapper);
}

Based on this, it can be seen that Spring Boot uses the current Message Converter to convert between objects (Geometry) and json. We only need to customize the objectMapper to support geojson.

Add third-party dependencies:

<dependency>
    <groupId>com.bedatadriven</groupId>
    <artifactId>jackson-datatype-jts</artifactId>
    <version>2.4</version>
</dependency>

<repositories>
    <repository>
        <id>sonatype-oss</id>
        <url>https://oss.sonatype.org/content/groups/public</url>
    </repository>
</repositories>

The custom object allows it to support the conversion between Geometry and geojson:

@Bean
public ObjectMapper objectMapper(){
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper().registerModule(new JtsModule());
    return objectMapper;

}

There is currently a bug in this third-party dependency that does not support the spatial coordinate system. In a single system, the coordinate system should be certain in general, so it is not a big problem to not support it, so we will modify the field mapping of the previous example to, :

//@Column(columnDefinition = "geometry(Point,4326)")
@Column(columnDefinition = "geometry(Point)")
private Point point;

Test controller:

@RestController
@Slf4j
@RequestMapping("/cities")
public class CityController {
    @Autowired
    CityRepository cityRepository;

    @PostMapping
    public City testIn(@RequestBody City city){
        log.info(city.getPoint().getSRID()
                + "/" + city.getPoint().getX()
                + "/" +city.getPoint().getY());
        return cityRepository.save(city);

    }
}

Test data, use the request body to send the POSTfollowing results to the background:

{
    "name": "南京",
    "point": {
      "type": "Point",
      "coordinates": [
        110.4,
        20.1
      ]
    }
  
  }

The return value is:

{
  "id": 58,
  "name": "南京",
  "point": {
    "type": "Point",
    "coordinates": [
      110.4,
      20.1
    ]
  }
}

At this time, we have realized the automatic conversion of Geometry and geojson data.

 

Source address:

http://www.wisely.top/2017/06/28/spring-boot-mvc-geometry-geojson/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326321709&siteId=291194637