问题
I am developing a GIS application with java(Spring-4.1.5 + Hibernate-4.3.8) and OpenLayers. For this project I use GeoTools-13RC
, HibernateSptial-4.3
, jts-1.13
and jackson-2.5
.
In this project, I have a layer in client side and in server, I save the features of this layer in a class. I defined the class below:
@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
*
*/
}
In start up of application, I need to init the layer in client side. for this, I need return from server side a json string to client for this layer. I don't want to use ST_AsGeoJson
or other matches. I use Spring REST controller for returning my Entity.
What do I do?
回答1:
You can use jackson-datatype-jts
from this link.
Its integration with spring is as below:
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());
}
}
Then in the rest controller if you return a Geometry, you can get its geojson.
回答2:
Returning a Response to the Client
You will need to create a resource to expose to your client. There is some good Spring documentation on this topic, and a couple of different ways to do it, but essentially something like this:
@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();
}
}
Building JSON
You should use Jackson to build your JSON. If you build a Spring resource then Jackson will likely be used by default when constructing a response. To give you an idea of how to translate an object to JSON manually:
@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();
}
来源:https://stackoverflow.com/questions/29739972/how-to-get-geojson-from-spring-rest-controller