【问题标题】:No suitable constructor found for type GeoJsonPoint找不到适合 GeoJsonPoint 类型的构造函数
【发布时间】:2015-07-21 07:58:50
【问题描述】:

很难弄清楚我做错了什么。可悲的是,我曾经让它工作过,但无法确定是什么改变了它。

据我了解,现在应该完全支持。

有问题的对象:

@Document
public class Place {

    public final static String URI = "/place";

    @Id private String id;

    private String name;

    private String caption;

    private GeoJsonPoint location;

    public Place() {}

    public Place(GeoJsonPoint geoJsonPoint) {
        this.location = geoJsonPoint;
    }

    // Proper getters & setters clipped.
}

调用(我的 Spring Boot 版本由于某种原因包含了额外的 x/y 坐标。)

{
    "id": null,
    "name": null,
    "caption": null,
    "location": {
        "x": 41.988161,
        "y": -87.6911499,
        "type": "Point",
        "coordinates": [
            41.988161,
            -87.6911499
        ]
    }
}

Pom(也许我有错误/冲突的依赖关系?)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>0.1.0</version>

    <properties>
        <start-class>com.nestorledon.hang2gether.core.Application</start-class>

        <!-- Includes new GeoJson support. -->
        <spring-data-releasetrain.version>Fowler-RELEASE</spring-data-releasetrain.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.M2</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test-mvc</artifactId>
            <version>1.0.0.M2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin> 
                <artifactId>maven-compiler-plugin</artifactId> 
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>

        <repository>
            <id>spring-milestone</id>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

【问题讨论】:

    标签: geojson spring-data-mongodb spring-data-rest spring-data-commons


    【解决方案1】:

    终于搞定了。

    创建了一个自定义的 JsonDeserializer

    public class GeoJsonDeserializer extends JsonDeserializer<GeoJsonPoint> {
    
        private final static String GEOJSON_TYPE_POINT = "Point";
    
        private final static String JSON_KEY_GEOJSON_TYPE = "type";
        private final static String JSON_KEY_GEOJSON_COORDS = "coordinates";
    
    
        @Override
        public GeoJsonPoint deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    
            final JsonNode tree = jp.getCodec().readTree(jp);
            final String type = tree.get(JSON_KEY_GEOJSON_TYPE).asText();
            final JsonNode coordsNode = tree.get(JSON_KEY_GEOJSON_COORDS);
    
            double x = 0;
            double y = 0;
            if(GEOJSON_TYPE_POINT.equalsIgnoreCase(type)) {
                x = coordsNode.get(0).asDouble();
                y = coordsNode.get(1).asDouble();
            }
    
            else {
                System.out.println(String.format("No logic present to deserialize %s ", tree.asText()));
            }
    
            final GeoJsonPoint point = new GeoJsonPoint(x, y);
    
            return point;
        }
    }
    

    然后我只是在属性本身上指定了 a 转换器。

    @JsonDeserialize(using = GeoJsonDeserializer.class)
    private GeoJsonPoint location;
    

    【讨论】:

      【解决方案2】:

      GeoJsonModule 像魅力一样工作 :)

      mapper.registerModule(new GeoJsonModule());
      

      【讨论】:

      • 好久没碰这个了!希望下次我重新访问它时就这么简单!
      【解决方案3】:

      Spring Data MongoDB 已经包含一个可以在jackson中注册的模块来反序列化GeoJsonPoint。

      要安装(在 Spring Boot 中),只需添加到您的配置类: @Bean public Module registerGeoJsonModule(){ return new GeoJsonModule(); }

      那么你应该可以使用反序列化:

      {
          "location":{
              "x":0,
              "y":0
          }
      }
      

      【讨论】:

      • 我有时间会试试这个。
      【解决方案4】:

      像这样你可以注册任何自定义的ObjectMapper:

      @Configuration
      public class HttpMessageConvertersConfig implements WebMvcConfigurer {
      
          @Override
          public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
              ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
                      .modules(new JavaTimeModule(), new Jdk8Module(), new GeoJsonModule())
                      .build();
              converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
          }
      
      }
      

      【讨论】:

      • 您好,您可以将文本格式化为代码,代码开头3个符号“`”,末尾3个“`”。
      【解决方案5】:

      把它放在@Configuration 类中

      @Bean
      public GeoJsonModule geoJsonModule() {
          return new GeoJsonModule();
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-01
        • 2019-09-28
        • 1970-01-01
        • 1970-01-01
        • 2012-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多