Why Jackson ObjectMapper cannot serialize play.data.validation.ValidationError class?

486 views
Skip to first unread message

Hossein Kazemi

unread,
Apr 19, 2014, 10:03:53 AM4/19/14
to play-fr...@googlegroups.com
I have the setup below below:

public class ValidationUtils {
  
  public static class ValidationResults {
      public List<ValidationError> validationErrors;
      public boolean valid;
      public ValidationResults(){
validationErrors = new ArrayList<>();
valid = true;
}
      @Override
      public String toString() {
return "ValidationResults [validationErrors=" + validationErrors
+ ", valid=" + valid + "]";
      }
  }

  // some static methods that create instances of ValidationResults.
}


Upon serialization of ValidationResults class I get this error:
<pre><code>com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class play.data.validation.ValidationError and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: utils.ValidationResults["validationErrors"]->java.util.ArrayList[0])


I use play.libs.Json utility class which is just a wraper around Jackson's Object Mapper.
Does anyone know what the problem is and how I can solve this?

The structure of ValidationError class is as follows:


package play.data.validation;

import java.util.*;

import com.google.common.collect.ImmutableList;

/**
 * A form validation error.
 */
public class ValidationError {
    
    private String key;
    private String message;
    private List<Object> arguments;

    /**
     * Constructs a new <code>ValidationError</code>.
     *
     * @param key the error key
     * @param message the error message
     */
    public ValidationError(String key, String message) {
        this(key, message, ImmutableList.of());
    }
    
    /**
     * Constructs a new <code>ValidationError</code>.
     *
     * @param key the error key
     * @param message the error message
     * @param arguments the error message arguments
     */
    public ValidationError(String key, String message, List<Object> arguments) {
        this.key = key;
        this.message = message;
        this.arguments = arguments;
    }
    
    /**
     * Returns the error key.
     */
    public String key() {
        return key;
    }
    
    /**
     * Returns the error message.
     */
    public String message() {
        return message;
    }

    /**
     * Returns the error arguments.
     */
    public List<Object> arguments() {
        return arguments;
    }
    
    public String toString() {
        return "ValidationError(" + key + "," + message + "," + arguments + ")";
    }
    
}

Alberto Souza

unread,
Apr 19, 2014, 12:06:19 PM4/19/14
to play-fr...@googlegroups.com
Hi, the problem is that methods from ValidationError are not java bean like. You have to configure Jackson to read private field too, and not only the getters... Here a example:

[code]

ValidationError e1 = new ValidationError("","");

ValidationError e2 = new ValidationError("","");

ValidationError e3 = new ValidationError("","");

ValidationError e4 = new ValidationError("","");

ValidationError e5 = new ValidationError("","");

ValidationError e6 = new ValidationError("","");

ArrayList<ValidationError> erros = new ArrayList<>(Arrays.asList(e1,e2,e3,e4,e5,e6));

ObjectMapper mapper = new ObjectMapper();

mapper.setVisibility(PropertyAccessor.ALL,Visibility.ANY);

Json.setObjectMapper(mapper);

System.out.println(Json.toJson(erros));

Reply all
Reply to author
Forward
0 new messages