Jackson – JsonMappingException (No serializer found for class)

1. Overview

In this quick tutorial, we will analyze the marshalling of entities with no getters and the solution for the Jackson JsonMappingException exception.

If you want to dig deeper and learn other cool things you can do with the Jackson 2 – head on over to the main Jackson tutorial.

2. The Problem

By default, Jackson 2 will only work with with fields that are either public, or have a public getter methods – serializing an entity that has all fields private or package private will fail:

public class MyDtoNoAccessors {
    String stringValue;
    int intValue;
    boolean booleanValue;

    public MyDtoNoAccessors() {
        super();
    }

    // no getters
}
@Test(expected = JsonMappingException.class)
public void givenObjectHasNoAccessors_whenSerializing_thenException() 
  throws JsonParseException, IOException {
    String dtoAsString = new ObjectMapper().writeValueAsString(new MyDtoNoAccessors());

    assertThat(dtoAsString, notNullValue());
}

The full exception is:

com.fasterxml.jackson.databind.JsonMappingException: 
No serializer found for class dtos.MyDtoNoAccessors 
and no properties discovered to create BeanSerializer 
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )

3. The Solution

The obvious solution is to add getters for the fields – if the entity is under our control. If that is not the case and modifying the source of the entity is not possible – then Jackson provides us with a few alternatives.

3.1. Globally Auto Detect Fields With Any Visibility

A first solution to this problem is to globally configure the ObjectMapper to detect all fields, regardless of their visibility:

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

This will allow the private and package private fields to be detected without getters, and serialization will work correctly:

@Test
public void givenObjectHasNoAccessors_whenSerializingWithAllFieldsDetected_thenNoException() 
  throws JsonParseException, IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());

    assertThat(dtoAsString, containsString("intValue"));
    assertThat(dtoAsString, containsString("stringValue"));
    assertThat(dtoAsString, containsString("booleanValue"));
}

3.2. Detected All Fields at the Class Level

Another option Jackson 2 provides is – instead of the global configuration – control the field visibility at the class level via the @JsonAutoDetect annotation:

@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class MyDtoNoAccessors { ... }

With this annotation, the serialization should now work correctly with this particular class:

@Test
public void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_thenNoException() 
  throws JsonParseException, IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());

    assertThat(dtoAsString, containsString("intValue"));
    assertThat(dtoAsString, containsString("stringValue"));
    assertThat(dtoAsString, containsString("booleanValue"));
}

4. Conclusion

This article illustrated how to get around the default field visibility in Jackson, by configuring a custom visibility either globally on the ObjectMapper or on individual classes. Jackson allows even further customization by providing options to control exactly how getters, setters or fields with specific visibilities are seen by the mapper.

The implementation of all these examples and code snippets can be found in my GitHub project – this is an Eclipse-based project, so it should be easy to import and run as it is.

Related posts:

Hướng dẫn Java Design Pattern – Factory Method
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Convert a Map to an Array, List or Set in Java
Comparing Dates in Java
Simplify the DAO with Spring and Java Generics
Java Optional as Return Type
Guide to BufferedReader
How to Get the Last Element of a Stream in Java?
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Spring JDBC
Java Program to Perform Arithmetic Operations on Numbers of Size
Lập trình đa luồng với Callable và Future trong Java
Java Program to Implement Bucket Sort
Call Methods at Runtime Using Java Reflection
Java Program to Implement the String Search Algorithm for Short Text Sizes
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java – InputStream to Reader
Check If Two Lists are Equal in Java
Introduction to Using Thymeleaf in Spring
Count Occurrences of a Char in a String
Jackson – Bidirectional Relationships
Java Program to Represent Graph Using Adjacency List
Java – Get Random Item/Element From a List
Một số tính năng mới về xử lý ngoại lệ trong Java 7
OAuth2.0 and Dynamic Client Registration
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Apache Commons Collections Bag
Lập trình đa luồng trong Java (Java Multi-threading)
Java – Delete a File
Java Program to Implement Skew Heap
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree