View Javadoc
1   /*
2    * Copyright 2022-2026 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.geojson.spring.boot.autoconfigure.jackson;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.bremersee.geojson.GeoJsonGeometryFactory;
22  import org.bremersee.geojson.GeoJsonObjectMapperModule;
23  import org.bremersee.geojson.spring.boot.autoconfigure.GeoJsonGeometryFactoryAutoConfiguration;
24  import org.bremersee.geojson.spring.boot.autoconfigure.GeoJsonProperties;
25  import org.springframework.beans.factory.ObjectProvider;
26  import org.springframework.boot.autoconfigure.AutoConfiguration;
27  import org.springframework.boot.autoconfigure.AutoConfigureAfter;
28  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
29  import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
30  import org.springframework.boot.context.event.ApplicationReadyEvent;
31  import org.springframework.boot.context.properties.EnableConfigurationProperties;
32  import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer;
33  import org.springframework.context.event.EventListener;
34  import org.springframework.util.ClassUtils;
35  import tools.jackson.databind.json.JsonMapper.Builder;
36  
37  /**
38   * The GeoJSON jackson 2 object mapper builder customizer.
39   *
40   * @author Christian Bremer
41   */
42  @ConditionalOnWebApplication
43  @ConditionalOnClass(name = {
44      "org.bremersee.geojson.GeoJsonObjectMapperModule",
45      "tools.jackson.databind.json.JsonMapper"})
46  @AutoConfiguration
47  @AutoConfigureAfter(GeoJsonGeometryFactoryAutoConfiguration.class)
48  @EnableConfigurationProperties(GeoJsonProperties.class)
49  public class GeoJsonJacksonJsonMapperBuilderCustomizer
50      implements JsonMapperBuilderCustomizer {
51  
52    private static final Log log = LogFactory
53        .getLog(GeoJsonJacksonJsonMapperBuilderCustomizer.class);
54  
55    private final GeoJsonProperties properties;
56  
57    private final GeoJsonGeometryFactory geometryFactory;
58  
59    /**
60     * Instantiates a new GeoJSON jackson 2 object mapper builder customizer.
61     *
62     * @param properties the properties
63     * @param geometryFactory the geometry factory
64     */
65    public GeoJsonJacksonJsonMapperBuilderCustomizer(
66        GeoJsonProperties properties,
67        ObjectProvider<GeoJsonGeometryFactory> geometryFactory) {
68      this.properties = properties;
69      this.geometryFactory = geometryFactory.getIfAvailable(GeoJsonGeometryFactory::new);
70    }
71  
72    /**
73     * Init.
74     */
75    @EventListener(ApplicationReadyEvent.class)
76    public void init() {
77      log.info(String.format("""
78              
79              *********************************************************************************
80              * %s
81              *********************************************************************************
82              * properties = %s
83              *********************************************************************************""",
84          ClassUtils.getUserClass(getClass()).getSimpleName(), properties));
85    }
86  
87    @Override
88    public void customize(Builder jsonMapperBuilder) {
89      GeoJsonObjectMapperModule module = new GeoJsonObjectMapperModule(
90          geometryFactory,
91          properties.isWithBoundingBox(),
92          properties.isUseBigDecimal()
93      );
94      jsonMapperBuilder.addModules(module);
95    }
96  }