简体   繁体   English

使用Jackson Json序列化无状态对象时发生异常

[英]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.

相关问题 使用Jackson将具有HashMap的对象序列化为JSON时映射异常 - Mapping Exception while serializing an object having a HashMap to JSON using Jackson 为什么在序列化/反序列化几何类型时杰克逊 JSON 映射异常 - Why Jackson JSON mapping exception when Serializing/Deserializing Geometry type JSON Jackson-使用自定义序列化程序序列化多态类时的异常 - JSON Jackson - exception when serializing a polymorphic class with custom serializer Jackson JSON 库:忽略序列化时导致异常的字段? - Jackson JSON library: Ignore fields that cause an exception when serializing? 序列化Hibernate对象时抛出奇怪的Jackson异常 - Strange Jackson exception being thrown when serializing Hibernate object 使用Jackson将Java对象序列化为JSON时,抑制包装器对象 - Suppress wrapper object when serializing Java object into JSON using Jackson 将 Hibernate 对象序列化为 JSON 时抛出异常 - Exception thrown when serializing Hibernate object to JSON 使用Jackson将通用java对象序列化为JSON - Serializing generic java object to JSON using Jackson JSON-序列化时,让Jackson使用JsonProperty - JSON - Get Jackson to use the JsonProperty when serializing 使用jackson数据绑定序列化对象时的Java InvalidDefinitionException - Java InvalidDefinitionException when serializing object with jackson databind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM