Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring boot上生成GeoJson?_Java_Mysql_Json_Spring - Fatal编程技术网

Java 如何在Spring boot上生成GeoJson?

Java 如何在Spring boot上生成GeoJson?,java,mysql,json,spring,Java,Mysql,Json,Spring,我正在尝试生成一个GeoJson,以便在LineString类型上发送一个Google地图(Google API)。我用的是弹簧靴 现在,我可以生成Json,但我找不到“转换”或“转换”Json的方法 我使用一个模型类从我的查询中发送数据(我使用MySQL)。因此,我的想法是通过讲座的要点(扫描仪名称)和相应的坐标 这是我的模型: package com.geologistic.model; public class PaqueteJson { private String nombr

我正在尝试生成一个GeoJson,以便在
LineString
类型上发送一个Google地图(Google API)。我用的是弹簧靴

现在,我可以生成Json,但我找不到“转换”或“转换”Json的方法

我使用一个模型类从我的查询中发送数据(我使用MySQL)。因此,我的想法是通过讲座的要点(扫描仪名称)和相应的坐标

这是我的模型:

package com.geologistic.model;

public class PaqueteJson {
    private String nombreEscaneo;
    private String latitud;
    private String longitud;
    public String getNombreEscaneo() {
        return nombreEscaneo;
    }
    public void setNombreEscaneo(String nombreEscaneo) {
        this.nombreEscaneo = nombreEscaneo;
    }
    public String getLatitud() {
        return latitud;
    }
    public void setLatitud(String latitud) {
        this.latitud = latitud;
    }
    public String getLongitud() {
        return longitud;
    }
    public void setLongitud(String longitud) {
        this.longitud = longitud;
    }
    public PaqueteJson() {
        super();
        // TODO Auto-generated constructor stub
    }
    public PaqueteJson(String nombreEscaneo, String latitud, String longitud) {
        super();
        this.nombreEscaneo = nombreEscaneo;
        this.latitud = latitud;
        this.longitud = longitud;
    }
    @Override
    public String toString() {
        return "PaqueteJson [nombreEscaneo=" + nombreEscaneo + ", latitud=" + latitud + ", longitud=" + longitud + "]";
    }


}
这是生成Consult和Json的查询

public List<PaqueteJson> findJson()
{
    List<PaqueteJson> paquetes= jdbcTemplate.query("select * from agencia ", new RowMapper<PaqueteJson>() {

        public PaqueteJson mapRow (ResultSet rs, int argl) throws SQLException{
            PaqueteJson paquete = new PaqueteJson (rs.getString("nombreEscaneo"),rs.getString("latitud"),
                                            rs.getString("longitud"));
            return paquete;
        }
        });
    return paquetes;

}

因此,我想返回一个GeoJson来将该GeoJson发送到Google API,但我找不到方法,我已经搜索了很多,我只是找到了一种方法,但是使用了Mongo DB(我使用的是MySQL)。

最后,我决定选择“功能集合”

解决方案

JSONObject featureCollection = new JSONObject();
        featureCollection.put("type", "FeatureCollection");
        JSONObject properties = new JSONObject();
        properties.put("name", "ESPG:4326");
        JSONObject crs = new JSONObject();
        crs.put("type", "name");
        crs.put("properties", properties);
        featureCollection.put("crs", crs);

        JSONArray features = new JSONArray();



        // ciclo for
        for (PaqueteJson obj : paqueteService.findJson()) {
            JSONObject feature = new JSONObject();
            feature.put("type", "Feature");
            //JSONArray JSONArrayCoord = new JSONArray();
            JSONObject geometry = new JSONObject();
            JSONObject desc = new JSONObject();
            JSONObject newFeature = new JSONObject();


            //JSONArrayCoord.put(0, Double.parseDouble(obj.getLongitud()));
            //JSONArrayCoord.put(1, Double.parseDouble(obj.getLatitud()));
            JSONArray JSONArrayCoord = new JSONArray("[" + obj.getLongitud() + "," + obj.getLatitud() + "]");

            geometry.put("type", "Point");
            geometry.put("coordinates", JSONArrayCoord);
            feature.put("geometry", geometry);
            // proper.put("properties", desc);
            feature.put("properties", desc);
            desc.put("name", obj.getNombreEscaneo());


            features.put(feature);
            featureCollection.put("features", features);

            // System.out.println(featureCollection.toString());
            // }

        }
        System.out.println(featureCollection.toString());
[{"nombreEscaneo":"SALIDAS ALBACETE","latitud":"39.018922","longitud":"-1.875926"},{"nombreEscaneo":"SALIDAS PLATAFORMA BAILEN","latitud":"38.085772","longitud":"-3.773147"}, ....
JSONObject featureCollection = new JSONObject();
        featureCollection.put("type", "FeatureCollection");
        JSONObject properties = new JSONObject();
        properties.put("name", "ESPG:4326");
        JSONObject crs = new JSONObject();
        crs.put("type", "name");
        crs.put("properties", properties);
        featureCollection.put("crs", crs);

        JSONArray features = new JSONArray();



        // ciclo for
        for (PaqueteJson obj : paqueteService.findJson()) {
            JSONObject feature = new JSONObject();
            feature.put("type", "Feature");
            //JSONArray JSONArrayCoord = new JSONArray();
            JSONObject geometry = new JSONObject();
            JSONObject desc = new JSONObject();
            JSONObject newFeature = new JSONObject();


            //JSONArrayCoord.put(0, Double.parseDouble(obj.getLongitud()));
            //JSONArrayCoord.put(1, Double.parseDouble(obj.getLatitud()));
            JSONArray JSONArrayCoord = new JSONArray("[" + obj.getLongitud() + "," + obj.getLatitud() + "]");

            geometry.put("type", "Point");
            geometry.put("coordinates", JSONArrayCoord);
            feature.put("geometry", geometry);
            // proper.put("properties", desc);
            feature.put("properties", desc);
            desc.put("name", obj.getNombreEscaneo());


            features.put(feature);
            featureCollection.put("features", features);

            // System.out.println(featureCollection.toString());
            // }

        }
        System.out.println(featureCollection.toString());