我有以下设置:
public class ValidationUtils {
public static class ValidationResults {
public List 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.
}
在ValidationResults类的序列化后,我收到此错误:
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])
我使用的是play.libs.Json实用程序类,它只是Jackson的Object Mapper的一个包装器。 有谁知道问题是什么以及如何解决这个问题?
ValidationError类的结构如下:
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 arguments;
/**
* Constructs a new ValidationError
.
*
* @param key the error key
* @param message the error message
*/
public ValidationError(String key, String message) {
this(key, message, ImmutableList.of());
}
/**
* Constructs a new ValidationError
.
*
* @param key the error key
* @param message the error message
* @param arguments the error message arguments
*/
public ValidationError(String key, String message, List 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 arguments() {
return arguments;
}
public String toString() {
return "ValidationError(" + key + "," + message + "," + arguments + ")";
}
}
答案 0 :(得分:0)
ValidationError的字段访问器方法既不遵循JavaBean conventions,也不注释@JsonProperty注释,因此Jackson无法识别它们。
以下是您稍微修改过的类的工作示例,其中包含更新的方法名称:
public class JacksonGetters {
public static class ValidationError {
private String key;
private String message;
private List arguments;
/**
* Constructs a new ValidationError.
*
* @param key the error key
* @param message the error message
*/
public ValidationError(String key, String message) {
this(key, message, null);
}
/**
* Constructs a new ValidationError.
*
* @param key the error key
* @param message the error message
* @param arguments the error message arguments
*/
public ValidationError(String key, String message, List arguments) {
this.key = key;
this.message = message;
this.arguments = arguments;
}
/**
* Returns the error key.
*/
public String getKey() {
return key;
}
/**
* Returns the error message.
*/
public String getMessage() {
return message;
}
/**
* Returns the error arguments.
*/
public List getArguments() {
return arguments;
}
public String toString() {
return "ValidationError(" + key + "," + message + "," + arguments + ")";
}
}
public static void main(String[] args) throws JsonProcessingException {
ValidationError err = new ValidationError("key", "message");
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(err));
}
}
输出:
{
"key" : "key",
"message" : "message",
"arguments" : null
}