1

我正在使用 java(Spring-4.1.5 + Hibernate-4.3.8) 和 OpenLayers 开发一个 GIS 应用程序。对于这个项目,我使用GeoTools-13RC、和。在这个项目中,我在客户端和服务器中都有一个层,我将该层的功能保存在一个类中。我定义了下面的类:HibernateSptial-4.3jts-1.13jackson-2.5

@Entity
@Table(name="MyPoint")
public class MyPoint
{
    @id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long Id;

    @Column
    private String name;

    @Type(type = "org.hibernate.spatial.GeometryType")
    @Column(name = "the_geom")
    private Point geometry;

    /*
    *
    Getter and Setter
    *
    */
}

在启动应用程序时,我需要在客户端初始化层。为此,我需要从服务器端向该层的客户端返回一个 json 字符串。我不想使用ST_AsGeoJson或其他匹配。我使用 Spring REST 控制器返回我的 Entity

我该怎么办

4

2 回答 2

0

向客户端返回响应

您将需要创建一个资源以向您的客户公开。关于这个主题有一些很好的Spring 文档,以及几种不同的方法,但基本上是这样的:

@Component
@Path("/my_points")
public class MyPoints {

    private PointService pointService;

    @GET
    @Path("{pointId}")
    public Response getPoint(@PathParam("pointId") String pointId) {
        return Response.ok(pointService.getById(pointId)).build();
    }
}

构建 JSON

您应该使用 Jackson 来构建您的 JSON。如果您构建一个 Spring 资源,那么在构建响应时可能会默认使用 Jackson。为了让您了解如何手动将对象转换为 JSON:

@Test
public void serializingAnObjectToJson() throws Exception {
    // Create a mapper that translates objects to json/xml/etc using Jackson.
    ObjectMapper om = new ObjectMapper();

    MyPoint point = new MyPoint(223l, "Superman");

    // Creates a JSON representation of the object
    String json = om.writeValueAsString(point);

    // Create a JAX-RS response with the JSON as the body
    Response response = Response.ok(json).build();
}
于 2015-04-20T06:21:09.910 回答
0

您可以jackson-datatype-jts从此链接使用。
它与spring的集成如下:

dispather-servlet.xml:

<mvc:annotation-driven >
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
          <property name="objectMapper" ref="customObjectMapper"/>
      </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
<bean id="customObjectMapper" class="my.server.util.CustomObjectMapper"/><!--custom jackson objectMapper -->

my.server.util.CustomObjectMapper:

package my.server.util;

import com.bedatadriven.jackson.datatype.jts.JtsModule;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.DefaultDeserializationContext;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import org.springframework.beans.factory.InitializingBean;

/**
 *
 * @author dariush
 */
public class CustomObjectMapper extends ObjectMapper implements InitializingBean{

   public CustomObjectMapper() {
   }

   public CustomObjectMapper(JsonFactory jf) {
       super(jf);
   }

   public CustomObjectMapper(ObjectMapper src) {
       super(src);
   }

   public CustomObjectMapper(JsonFactory jf, DefaultSerializerProvider sp, DefaultDeserializationContext dc) {
       super(jf, sp, dc);
   }

   @Override
   public void afterPropertiesSet() throws Exception {
       this.registerModule(new JtsModule());
   }

}

然后在 rest 控制器中,如果你返回一个 Geometry,你可以得到它的 geojson。

于 2015-04-20T08:31:18.703 回答