How to get geoJSON from spring REST Controller?

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 on the client side and on the server, I save the functions of this layer in the class. I have 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 * */ } 

When starting the application, I need to create a client-side layer. for this I need to return the server side json string for the client for this layer. I do not want to use ST_AsGeoJson or other matches. I am using a Spring REST controller to return my object .

What do i do ?

+1
source share
2 answers

Return reply to customer

You will need to create a resource for your client. There is good Spring documentation on this topic in this thread and a few different ways to do this, but basically 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 must use Jackson to create your JSON. If you create a Spring resource, then Jackson will be used by default when building the response. To give you an idea of ​​how to manually translate an object into 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(); } 
0
source

You can use jackson-datatype-jts from this link .
Its integration with spring looks like this:

servlet.xml helper:

 <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 of the controller, if you return the geometry, you can get it geojson.

0
source

Source: https://habr.com/ru/post/1205181/


All Articles