I get the an exception when trying to serialize a very simple object using Jackson. The error:
我在尝试使用Jackson序列化一个非常简单的对象时遇到了一个例外。错误:
org.codehaus.jackson.map.JsonMappingException: No serializer found for class MyPackage.TestA and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
org.codehaus.jackson.map。JsonMappingException:没有为类MyPackage找到序列化器。TestA和没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS)
Below is the simple class and code to serialize.
下面是要序列化的简单类和代码。
Can anyone tell my why I get this error?
有人能告诉我为什么会有这个错误吗?
public class TestA {
String SomeString = "asd";
}
TestA testA = new TestA();
ObjectMapper om = new ObjectMapper();
try {
String testAString = om.writeValueAsString(testA); // error here!
TestA newTestA = om.readValue(testAString, TestA.class);
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
11 个解决方案
#1
230
As already described, the default configuration of an ObjectMapper
instance is to only access properties that are public fields or have public getters/setters. An alternative to changing the class definition to make a field public or to provide a public getter/setter is to specify (to the underlying VisibilityChecker
) a different property visibility rule. Jackson 1.9 provides the ObjectMapper.setVisibility()
convenience method for doing so. For the example in the original question, I'd likely configure this as
如前所述,ObjectMapper实例的默认配置是仅访问公共字段或具有公共getter /setter的属性。更改类定义以使字段公共或提供公共getter/setter的替代方法是(对底层的VisibilityChecker)指定不同的属性可见性规则。Jackson 1.9提供了ObjectMapper.setVisibility()便利方法。对于原始问题中的示例,我可能将其配置为
myObjectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
For more information and details on related configuration options, I recommend reviewing the JavaDocs on ObjectMapper.setVisibility()
.
有关相关配置选项的更多信息和细节,我建议您查看ObjectMapper.setVisibility()上的JavaDocs。
#2
56
For Jackson to serialize that class, the SomeString
field needs to either be public
(right now it's package level isolation) or you need to define getter and setter methods for it.
对于Jackson要序列化这个类,SomeString字段需要要么是公共的(现在是包级隔离),要么需要为它定义getter和setter方法。
#3
22
I have the same problem in my source code. I just added
我的源代码中也有同样的问题。我只是说
getter
getter
and
和
setter
setter
the problem solved.
这个问题解决了。
#4
16
The problem in my case was Jackson was trying to serialize an empty object with no attributes nor methods.
在我的例子中,问题是Jackson试图序列化一个没有属性或方法的空对象。
As suggested in the exception I added the following line to avoid failure on empty beans:
正如在异常中所建议的,我添加了以下行以避免在空bean上失败:
For Jackson 1.9
杰克逊1.9
myObjectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
For Jackson 2.X
为杰克逊2.倍
myObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
You can find a simple example on jackson disable fail_on_empty_beans
您可以在jackson禁用fail_on_empty_bean上找到一个简单的示例
#5
5
If you can edit the class containing that object, I usually just add the annotation
如果您可以编辑包含该对象的类,我通常只添加注释。
import com.fasterxml.jackson.annotation.JsonIgnore;
@JsonIgnore
NonSerializeableClass obj;
#6
4
This error is also thrown if you forget to add the .build() method onto your return status.
如果忘记将.build()方法添加到返回状态,也会抛出此错误。
return Response.status(Status.OK); // fails
return Response.status(Status.OK).build(); // works
I got the following error without build() method:
我没有构建()方法得到以下错误:
org.codehaus.jackson.map.JsonMappingException: No serializer found for class com.sun.jersey.core.spi.factory.ResponseBuilderImpl
#7
3
For Oracle Java applications, add this after the ObjectMapper
instantiation:
对于Oracle Java应用程序,在ObjectMapper实例化后添加以下内容:
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
#8
3
I had the same problem for a child class where I had control, object mapper was in a common module and was inaccessible. I solved it by adding this annotation for my child class whose object was to be serialized.
对于有控件的子类,我也遇到了同样的问题,对象映射器位于公共模块中,无法访问。我通过为要序列化的子类添加这个注释来解决这个问题。
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
#9
2
The problem may be because you have declared variable as private
. If you change it to public
, it works.
问题可能在于您已经将变量声明为private。如果你把它变成公共的,它就会起作用。
Better option is to use getter and setter methods for it.
更好的选择是使用getter和setter方法。
This will solve the issue!
这将解决问题!
#10
1
I had a similar problem with lazy loading via the hibernate proxy object. Got around it by annotating the class having lazy loaded private properties with:
通过hibernate代理对象延迟加载也有类似的问题。通过注释具有惰性加载的私有属性的类来绕过它:
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
#11
0
Though I added getters and setters I was getting the same error. Later I found a bug, that is by Sonar's advice I cgahnged the getters and setters as protected which was causing the issue. Once I fixed that it worked as exppected.
虽然我添加了getter和setter,但是我得到了相同的错误。后来我发现了一个bug,根据Sonar的建议,我将getter和setter作为保护,从而导致了这个问题。一旦我确定它是有效的。
#1
230
As already described, the default configuration of an ObjectMapper
instance is to only access properties that are public fields or have public getters/setters. An alternative to changing the class definition to make a field public or to provide a public getter/setter is to specify (to the underlying VisibilityChecker
) a different property visibility rule. Jackson 1.9 provides the ObjectMapper.setVisibility()
convenience method for doing so. For the example in the original question, I'd likely configure this as
如前所述,ObjectMapper实例的默认配置是仅访问公共字段或具有公共getter /setter的属性。更改类定义以使字段公共或提供公共getter/setter的替代方法是(对底层的VisibilityChecker)指定不同的属性可见性规则。Jackson 1.9提供了ObjectMapper.setVisibility()便利方法。对于原始问题中的示例,我可能将其配置为
myObjectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
For more information and details on related configuration options, I recommend reviewing the JavaDocs on ObjectMapper.setVisibility()
.
有关相关配置选项的更多信息和细节,我建议您查看ObjectMapper.setVisibility()上的JavaDocs。
#2
56
For Jackson to serialize that class, the SomeString
field needs to either be public
(right now it's package level isolation) or you need to define getter and setter methods for it.
对于Jackson要序列化这个类,SomeString字段需要要么是公共的(现在是包级隔离),要么需要为它定义getter和setter方法。
#3
22
I have the same problem in my source code. I just added
我的源代码中也有同样的问题。我只是说
getter
getter
and
和
setter
setter
the problem solved.
这个问题解决了。
#4
16
The problem in my case was Jackson was trying to serialize an empty object with no attributes nor methods.
在我的例子中,问题是Jackson试图序列化一个没有属性或方法的空对象。
As suggested in the exception I added the following line to avoid failure on empty beans:
正如在异常中所建议的,我添加了以下行以避免在空bean上失败:
For Jackson 1.9
杰克逊1.9
myObjectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
For Jackson 2.X
为杰克逊2.倍
myObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
You can find a simple example on jackson disable fail_on_empty_beans
您可以在jackson禁用fail_on_empty_bean上找到一个简单的示例
#5
5
If you can edit the class containing that object, I usually just add the annotation
如果您可以编辑包含该对象的类,我通常只添加注释。
import com.fasterxml.jackson.annotation.JsonIgnore;
@JsonIgnore
NonSerializeableClass obj;
#6
4
This error is also thrown if you forget to add the .build() method onto your return status.
如果忘记将.build()方法添加到返回状态,也会抛出此错误。
return Response.status(Status.OK); // fails
return Response.status(Status.OK).build(); // works
I got the following error without build() method:
我没有构建()方法得到以下错误:
org.codehaus.jackson.map.JsonMappingException: No serializer found for class com.sun.jersey.core.spi.factory.ResponseBuilderImpl
#7
3
For Oracle Java applications, add this after the ObjectMapper
instantiation:
对于Oracle Java应用程序,在ObjectMapper实例化后添加以下内容:
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
#8
3
I had the same problem for a child class where I had control, object mapper was in a common module and was inaccessible. I solved it by adding this annotation for my child class whose object was to be serialized.
对于有控件的子类,我也遇到了同样的问题,对象映射器位于公共模块中,无法访问。我通过为要序列化的子类添加这个注释来解决这个问题。
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
#9
2
The problem may be because you have declared variable as private
. If you change it to public
, it works.
问题可能在于您已经将变量声明为private。如果你把它变成公共的,它就会起作用。
Better option is to use getter and setter methods for it.
更好的选择是使用getter和setter方法。
This will solve the issue!
这将解决问题!
#10
1
I had a similar problem with lazy loading via the hibernate proxy object. Got around it by annotating the class having lazy loaded private properties with:
通过hibernate代理对象延迟加载也有类似的问题。通过注释具有惰性加载的私有属性的类来绕过它:
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
#11
0
Though I added getters and setters I was getting the same error. Later I found a bug, that is by Sonar's advice I cgahnged the getters and setters as protected which was causing the issue. Once I fixed that it worked as exppected.
虽然我添加了getter和setter,但是我得到了相同的错误。后来我发现了一个bug,根据Sonar的建议,我将getter和setter作为保护,从而导致了这个问题。一旦我确定它是有效的。