【发布时间】:2015-06-26 12:59:42
【问题描述】:
我有一个来自 spring 框架的名为 GeoJsonPoint 的对象,在我的集成测试中,jackson mapper 无法对其进行反序列化。此外,我不能添加一个虚拟构造函数,因为它是一个外部对象。所以我被困住了。这是我的主要实体;
@Document(collection = "foodTrucks")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)
public class FoodTruckEntity {
@Id
private ObjectId id;
private String applicant;
private Status status;
private String[] foodItems;
private Double longitude;
private Double latitude;
private GeoJsonPoint geoJsonPoint;
public FoodTruckEntity() {};
// getters and setters
}
还有测试
@Test
public void test() {
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
String getNearFoodTrucksUrl = "http://localhost:8080/food-truck/near-locations/longitude/-122.398658184604/latitude/37.7901490737255/findAll";
WebResource webResource = client.resource(getNearFoodTrucksUrl);
ClientResponse response = webResource.get(ClientResponse.class);
GeoResults<FoodTruckEntity> geoResults = webResource.get(new GenericType<GeoResults<FoodTruckEntity>>(){});
if (response.getStatus() != 200) {
throw new WebApplicationException();
}
}
我得到的错误;
com.sun.jersey.api.client.ClientHandlerException: org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class org.springframework.data.geo.GeoResults<entity.FoodTruckEntity>]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@107ed6fc; line: 1, column: 2]
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:644)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:604)
编辑:这是我拥有的球衣的依赖项
<!-- JERSEY JSON -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
<!-- JERSEY JSON -->
EDIT_2:作为字符串的响应如下所示,
{
"averageDistance":{
"value":0.0,
"metric":"MILES",
"normalizedValue":0.0,
"unit":"mi"
},
"content":[
{
"content":{
"id":{
"timestamp":1429498845,
"machineIdentifier":11487078,
"processIdentifier":1432,
"counter":9275496,
"time":1429498845000,
"date":1429498845000,
"timeSecond":1429498845
},
"applicant":"Cupkates Bakery, LLC",
"facilityType":"Truck",
"status":"APPROVED",
"foodItems":[
"Cupcakes"
],
"longitude":-122.398658184604,
"latitude":37.7901490737255,
"geoJsonPoint":{
"x":-122.398658184604,
"y":37.7901490737255,
"type":"Point",
"coordinates":[
-122.398658184604,
37.7901490737255
]
}
},
"distance":{
"value":0.0,
"metric":"MILES",
"normalizedValue":0.0,
"unit":"mi"
}
}
]
}
【问题讨论】:
-
我从未使用过这个 GeoResults API 。你能发布字符串结果
webresource.get(String.class)。我很好奇 JSON 长什么样。 -
@peeskillet 当然。我已在我的问题中将非常简单的响应作为字符串发布。
-
@peeskillet 我已经在我的问题中以字符串形式发布了非常简单的响应。
-
GeoJsonPoint 的构造函数需要传递给它的 x、y 值。如何定义自己的 GeoJsonPoint 类并删除对 Spring 的 GeoJsonPoint 的引用
-
@faljbour 感谢您的回复。我以前想过,但我必须在查询数据库时使用 GeoJsonPoint。所以,我不得不搞砸后端。我将尝试将结果解析为一个字符串,这似乎是一个相当不错的选择。