[英]Exception when serializing a stateless object with jackson Json
I have been using the jackson implementation of the json protocol in my own little project, and it has gone just fine a while now until I decided (for the first time) to serialize a stateless object. 我在我自己的小项目中一直在使用json协议的jackson实现,现在已经进行了一段时间,直到我(第一次)决定序列化无状态对象。
I know that might sound weird, why would I want to send a stateless object? 我知道这听起来很奇怪,为什么我要发送无状态对象? What I serialize is requests for a server, and this particular one conatins no fields, just code for an instruction on the server side.
我要序列化的是对服务器的请求,而这个特殊的请求不包含任何字段,而只是在服务器端为指令编写代码。 My model can take any ClientRequest implementation and call it's perform() method.
我的模型可以采用任何ClientRequest实现,并将其称为perform()方法。 I want it to work even though the request comes without fields.
我希望它能够工作,即使请求没有字段。
Code looks like this: 代码如下:
public class GetWallInputsRequest implements ClientRequest<List<WallInput>>
{
@JsonCreator public GetWallInputsRequest()
{
}
@Override public ServerResponse<List<WallInput>> perform()
{
return new WallMessageResponse( Wall.WALL.getInputs() );
}
}
I get JsonMappingException: No serializer found for class GetWallInputsRequest. 我收到JsonMappingException:没有为类GetWallInputsRequest找到序列化程序。 Google does not help me, which makes me wonder if I'm just being stupid.
Google不能帮我,这让我想知道我是否只是愚蠢。 Sadly I don't see a way out of this.
可悲的是,我看不出有什么办法。
I solved it after a lot of brute force attempting different things. 经过大量蛮力尝试不同的事情后,我解决了它。 And by solved it I mean not figured it out but made it work.
通过解决这个问题,我的意思是不弄清楚,但是让它起作用了。 By adding the line:
通过添加以下行:
@JsonAutoDetect(getterVisibility=JsonAutoDetect.Visibility.NONE)
above the class declaration it seems to work out. 在类声明之上,它似乎可以解决。 Why this is necessary I don't know, but now It sends an empty json string instead of crashing.
我不知道为什么需要这样做,但是现在它发送一个空的json字符串而不是崩溃。
The documentation says 该文件说
Value that indicates that no access modifiers are auto-detectable: this can be used to explicitly disable auto-detection for specified types.
指示没有访问修饰符可以自动检测的值:可以用于显式禁用指定类型的自动检测。
Since your class doesn't include any explicit notations to tell Jackson that there's a field or method to serialize, it determines that there is indeed nothing to look for. 由于您的班级没有任何明确的表示法来告诉杰克逊有一个要序列化的字段或方法,因此它确定确实没有要查找的东西。 Without this, I presume, it's going to expect something , as suggested in the documentation quoted.
我想如果没有这个,我将期望某些东西 ,如引用的文档中所建议。
http://jackson.codehaus.org/1.9.0/javadoc/org/codehaus/jackson/annotate/JsonAutoDetect.Visibility.html http://jackson.codehaus.org/1.9.0/javadoc/org/codehaus/jackson/annotate/JsonAutoDetect.Visibility.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.