Java 意外的输入结束:OBJECT 的预期关闭标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25526611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:35:02  来源:igfitidea点击:

Unexpected end-of-input: expected close marker for OBJECT

javagoogle-app-enginesearchHymansongeopoints

提问by Lucía Román González

I'm trying to create a geopoint in my app engine application but when I try to deserialize it I got this annoying message:

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

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

This is my JSON code:

这是我的 JSON 代码:

{
    "id": 31,
    "name": "pepe",
    "mail": "[email protected]",
    "password": 123,
    "age":10,
    "birthday": "01-06-1991",
    "desc" : " bla bla",
    "gp": {
     "latitude": 64.124596,
     "longitude": -147.8632
     }
}

and this is the declaration of geopoint and my custom deserialize method:

这是 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);
    }
}

采纳答案by Micha? Ziober

It looks like your GeoPointclass has constructor like this: public GeoPoint(double latitude, double longitude). In this case we can use MixIn feature. We have to create additional abstract class in which we will provide mapping between your POJOand JSON.

它看起来像你的GeoPoint类有构造是这样的:public GeoPoint(double latitude, double longitude)。在这种情况下,我们可以使用MixIn 功能。我们必须创建额外的抽象类,我们将在其中提供您POJOJSON.

abstract class GeoPointMixIn {

    GeoPointMixIn(@JsonProperty("latitude") double latitude, @JsonProperty("longitude") double longitude) {

    }
}

And simple usage:

和简单的用法:

ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(GeoPoint.class, GeoPointMixIn.class);

System.out.println(mapper.readValue(JSON, YourRootPojo.class));

As you can see you do not have to implement custom deserializer for GeoPointclass.

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

回答by Irina

For me it was much simple solution.

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

I forgot to close brackets in my SwaggerUI request like that:

我忘记在我的 SwaggerUI 请求中关闭括号,如下所示:

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