1

I'm trying to serialize an object in Java using Jackson, but when I'm trying to serialize it, it gives me this error:

No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer

I tried this post, but it didn't help.

Here is the class I'm trying to serialize:

public class Repository {
    public String name;
    @JsonIgnore   // to avoid recursive calls
    public ArrayList<UserRole> contributors = new ArrayList<UserRole>();
    public User self;
    public ArrayList<FileInfo> files;
    public RepositoryType repositoryType;
    public String path;
}

I also tried to create getters/setters for each field but still nothing.

Here is my serialization method:

public static String convertObjectToJson(Object object) throws IOException {
        ObjectWriter objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String json = objectWriter.writeValueAsString(object); //error on this line
        return json;
    } 
Community
  • 1
  • 1
Mohammad Siavashi
  • 1,192
  • 2
  • 17
  • 48

2 Answers2

3

Looks like your one of your classes has java.io.FileDescriptor reference.

By default, Jackson 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

If you look at the source code of java.io.FileDescriptor you can see there are private fields without public getters.

You should configure your objectMapper visibility to allow access to private fields also.

// For jackson 2.*
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

// For jackson lower than 2
objectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
Ghokun
  • 3,345
  • 3
  • 26
  • 30
0

I was facing problems to send objects to Thymeleaf template with ResponseEntity it was giving me exception "StackOverFlowError" while serializing and your note " @JsonIgnore // to avoid recursive calls" solved my problem. Thanks

Omar
  • 64
  • 4
  • 1
    This does not provide an answer to the question. You can [search for similar questions](//stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](//stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](//stackoverflow.com/tour) – u32i64 Jul 21 '17 at 11:07