ModelMapper:将几何图形映射为GeoJson

时间:2018-01-26 15:55:48

标签: spring-boot jackson geojson modelmapper

我刚刚发现了ModelMapper,我发现它对生成DTO非常有用。

无论如何,当序列化拥有JTS Geometry数据类型属性的对象时,我希望它们是GeoJson格式。

暂时,以下课程......

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.vividsolutions.jts.geom.MultiPolygon;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "municipios")
@Builder
@Data
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class Municipio implements Serializable {

    /*
     * Atributos
     */
    @Transient
    private static final long serialVersionUID = -2486158354849124667L;

    @Id
    private Integer gid;
    private String codigo;
    private String texto;
    private MultiPolygon geom;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(
            name = "cod_prov", 
            referencedColumnName = "codigo"
        )
    private Provincia provincia;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(
            name = "cod_ccaa", 
            referencedColumnName = "codigo"
        )
    private ComunidadAutonoma comunidad;

}

...被序列化为......

    "municipio": {
    "gid": 27,
    "codigo": "01036",
    "texto": "Laudio/Llodio",
    "provincia": {
        "gid": 1,
        "codigo": "01",
        "texto": "Álava",
        "texto_alt": "Araba",
        "comunidad": {
            "gid": 16,
            "codigo": "16",
            "texto": "País Vasco",
            "texto_alt": "Euskadi"
        }
    },
    "geom": "MULTIPOLYGON (((-2.9420491414616916 43.14380029306756, -2.9408165999002556 43.14430841999599, -2.939059950482368 43.141709610211024, -2.94050966386134 43.13799144046195, -2.936693413950487 43.13417138986535, -2.942182362664065 43.130752517734585, -2.943746497171919 43.126998320661244, -2.9491831049375983 43.122917261507745, -2.9568712631986553 43.11281715256084, -2.958883438671909 43.11305654097181, -2.9659615390025005 43.103108652799214, -2.9652067663290955 43.10264468267702, -2.9762492404051137 43.097150217253514, -2.9899968551814102 43.1013215781132, -2.9963369436287586 43.10706705889331, -3.0097035041930043 43.11278484125608, -3.0129149314121726 43.116877214910765, -3.017330715593204 43.116457973261085, -3.019790265011745 43.11919501110562, -3.02040901781778 43.12611504150391, -3.02393972418118 43.12847813412591, -3.025215180146138 43.13126035313113, -3.021643236846659 43.137154623532716, -3.013836762498057 43.1408026665335, -3.005568207272493 43.148466258438624, -3.0026147541242167 43.14959643860667, -2.9909328393119017 43.1625087272497, -2.9796315563151357 43.17050333013982, -2.9766664577130144 43.17485199744999, -2.9736646499823842 43.17600841462698, -2.979262791265818 43.18166902950952, -2.9742758156428035 43.18436486223879, -2.97110139733367 43.18276575229797, -2.969942290790593 43.179992017776314, -2.961893498167311 43.17338464369854, -2.957182021700495 43.17193318237574, -2.955167271064855 43.170041426431084, -2.9475444948015594 43.16848931161088, -2.955018585037522 43.157632918409604, -2.9474297891472103 43.151515433493785, -2.9420491414616916 43.14380029306756)))"
}

在序列化对象时,有没有办法告诉ModelMapper将Geometries映射为GeoJSON对象?

0 个答案:

没有答案