【问题标题】:Unexpected end-of-input: expected close marker for OBJECT意外的输入结束:OBJECT 的预期关闭标记
【发布时间】:2014-08-27 11:59:21
【问题描述】:

我正在尝试在我的应用引擎应用程序中创建一个地理点,但是当我尝试反序列化它时,我收到了这条烦人的消息:

Uncaught exception from servlet
org.codehaus.jackson.JsonParseException: Unexpected end-of-input: expected close marker for OBJECT (from [Source: java.io.StringReader@1a21658; line: 1, column: 0]).

这是我的 JSON 代码:

{
    "id": 31,
    "name": "pepe",
    "mail": "p@p.com",
    "password": 123,
    "age":10,
    "birthday": "01-06-1991",
    "desc" : " bla bla",
    "gp": {
     "latitude": 64.124596,
     "longitude": -147.8632
     }
}

这是geopoint的声明和我的自定义反序列化方法:

GeoPoint gp;

public GeoPoint getGp() {
    return gp;
}

@JsonDeserialize(using = CustomGeoPintDeserializer.class)
public void setGp(GeoPoint gp) {
    this.gp = gp;
}

public  class CustomGeoPintDeserializer extends JsonDeserializer<GeoPoint> {

    @Override
    public GeoPoint deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {

        System.out.println("ENTRA");
        ObjectMapper mapper = new ObjectMapper();
        JsonNode actualObj = mapper.readValue(jsonParser.getText(), JsonNode.class);

        double latitude = actualObj.get("latitude").getDoubleValue();

        double longitude = actualObj.get("longitude").getDoubleValue();


        return new GeoPoint(latitude,longitude);
    }
}

【问题讨论】:

  • GeoPoint 类的外观如何?这个类的包是什么?

标签: java google-app-engine search jackson geopoints


【解决方案1】:

对我来说,这是非常简单的解决方案。

我忘记在我的 SwaggerUI 请求中关闭括号:

{
    "jobGroup": "Authorizer-CCC",
    "jobName": "Authorizer-2NO5-101"

【讨论】:

    【解决方案2】:

    看起来你的GeoPoint 类有这样的构造函数:public GeoPoint(double latitude, double longitude)。在这种情况下,我们可以使用MixIn feature。我们必须创建额外的抽象类,我们将在其中提供您的POJOJSON 之间的映射。

    abstract class GeoPointMixIn {
    
        GeoPointMixIn(@JsonProperty("latitude") double latitude, @JsonProperty("longitude") double longitude) {
    
        }
    }
    

    以及简单的用法:

    ObjectMapper mapper = new ObjectMapper();
    mapper.addMixInAnnotations(GeoPoint.class, GeoPointMixIn.class);
    
    System.out.println(mapper.readValue(JSON, YourRootPojo.class));
    

    如您所见,您不必为 GeoPoint 类实现自定义反序列化器。

    【讨论】:

    • 使用 mapper.addMixInAnnotations 需要哪个版本的 jackson?
    • 我使用的是 2.4.1 版(fasterxml),但我认为这种方法从 2.0.0 版开始就存在了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2018-08-07
    • 2015-08-31
    相关资源
    最近更新 更多