您可以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。