Serializing with Jackson (JSON) - getting "No serializer found"?

JavaJsonJackson

Java Problem Overview


I get the an exception when trying to serialize a very simple object using Jackson. The error:

> 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) )

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();
}

Java Solutions


Solution 1 - Java

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

myObjectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);

For Jackson >2.0:

myObjectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

For more information and details on related configuration options, I recommend reviewing the JavaDocs on ObjectMapper.setVisibility().

Solution 2 - Java

Add a > getter

and a

> setter

and the problem is solved.

Solution 3 - Java

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.

Solution 4 - Java

The problem in my case was Jackson was trying to serialize an empty object with no attributes nor methods.

As suggested in the exception I added the following line to avoid failure on empty beans:

For Jackson 1.9

myObjectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);

For Jackson 2.X

myObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

You can find a simple example on https://stackoverflow.com/questions/15261456/jackson-disable-fail-on-empty-beans

Solution 5 - Java

If you can edit the class containing that object, I usually just add the annotation

import com.fasterxml.jackson.annotation.JsonIgnore;

@JsonIgnore 
NonSerializeableClass obj;

Solution 6 - Java

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)

Solution 7 - Java

This error is also thrown if you forget to add the .build() method onto your return status.

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

Solution 8 - Java

SpringBoot2.0 ,I resolve it by follow code:

@Bean public ObjectMapper objectMapper() {
 return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);}

Solution 9 - Java

Jackson uses getters and setters serialization./deserialization. So add getter and setter in your model class.

Solution 10 - Java

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:

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

Solution 11 - Java

For Oracle Java applications, add this after the ObjectMapper instantiation:

mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    

Solution 12 - Java

I found at least three ways of doing this:

  1. Add public getters on your entity that you try to serialize;
  2. Add an annotation at the top of the entity, if you don't want public getters. This will change the default for Jackson from Visbility=DEFAULT to @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) where any access modifiers are accepted;
  3. Change your ObjectMapper globally by setting objectMapperInstance.setVisibility(JsonMethod.FIELD, Visibility.ANY);. This should be avoided if you don't need this functionality globally.

Choose based on your needs.

Solution 13 - Java

The problem may be because you have declared variable as private. If you change it to public, it works.

Better option is to use getter and setter methods for it.

This will solve the issue!

Solution 14 - Java

Here are the three options:

  1. Data/class that's been accessed needs to be public
  2. If not public, add getters & setters
  3. Or add @JsonIgnore("context")

Solution 15 - Java

Please use this at class level for the bean:

@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})

Solution 16 - Java

I just had only getters , after adding setters too , problems resolved.

Solution 17 - Java

adding setter and getter will also solve the issue as it fixed for me. For Ex:

public class TestA {
    String SomeString = "asd";
    
    public String getSomeString () { 		return SomeString ; 	}

	public void setSomeString (String SS ) { 		SomeString = SS ; 	} 
}

Solution 18 - Java

in spring boot 2.2.5

after adding getter and setter

i added @JsonIgnore on top of the field.

Solution 19 - Java

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.

Solution 20 - Java

If you use Lomdok libraray (https://projectlombok.org/) then add @Data (https://projectlombok.org/features/Data) annotation to your data object class.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTedView Question on Stackoverflow
Solution 1 - JavaProgrammer BruceView Answer on Stackoverflow
Solution 2 - JavaGünay GültekinView Answer on Stackoverflow
Solution 3 - JavaChrisView Answer on Stackoverflow
Solution 4 - JavaMartín CView Answer on Stackoverflow
Solution 5 - JavaZiglioUKView Answer on Stackoverflow
Solution 6 - JavaPavan KumarView Answer on Stackoverflow
Solution 7 - JavapatricsView Answer on Stackoverflow
Solution 8 - JavaJanusView Answer on Stackoverflow
Solution 9 - JavaNakeshView Answer on Stackoverflow
Solution 10 - JavaN. berouainView Answer on Stackoverflow
Solution 11 - JavaLEMUEL ADANEView Answer on Stackoverflow
Solution 12 - JavageorgerView Answer on Stackoverflow
Solution 13 - JavaSanchit SinghaniaView Answer on Stackoverflow
Solution 14 - JavaSanthosh KumarView Answer on Stackoverflow
Solution 15 - Javaraveendra_bikkinaView Answer on Stackoverflow
Solution 16 - JavaVanita DhotreView Answer on Stackoverflow
Solution 17 - JavaSatya PrakashView Answer on Stackoverflow
Solution 18 - JavasabaView Answer on Stackoverflow
Solution 19 - JavayellowView Answer on Stackoverflow
Solution 20 - JavaAndriy KryvtsunView Answer on Stackoverflow