2012-04-25 4 views
2

안녕하세요, 잭슨을 직렬화하고 보호 된 생성자와 클래스 (SimpleExpression) deserialize 위해 노력하고있어. gson을 사용할 때 아무 문제가 없지만 jackson이 보호 된 생성자를 처리 할 수없는 것 같습니다. mixin-annotations를 사용해 보았지만 일하지 않았습니다. 직렬화는 문제없이 작동합니다. Jackson throws :잭슨과 보호 된 생성자를 deserialize

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type 

도움이 필요하십니까? 내 코드 :

private static SimpleExpression create(String json) throws JsonParseException, JsonMappingException, IOException 
{ 
    ObjectMapper mapper = new ObjectMapper().setVisibility(PropertyAccessor.ALL, Visibility.ANY); 
    SimpleExpression sp = null; 
    sp = mapper.readValue(json, SimpleExpression.class); 
    return sp; 
} 

SimpleExpression 클래스는, 나는 getter와 setter를 omiting하고 있습니다.

public class SimpleExpression implements Criterion 
{ 
private static final long serialVersionUID = 1L; 

private final String propertyName; 

private final Object value; 

private boolean ignoreCase; 

private final String op; 

private final String type; 

protected SimpleExpression(String propertyName, Object value, String op) 
{ 
    this.propertyName = propertyName; 
    this.value = value; 
    this.op = op; 
    this.type = value.getClass().getName(); 
} 

protected SimpleExpression(String propertyName, Object value, String op, boolean ignoreCase) 
{ 
    this.propertyName = propertyName; 
    this.value = value; 
    this.ignoreCase = ignoreCase; 
    this.op = op; 
    this.type = value.getClass().getName(); 
} 
} 
+0

당신은 SimpleExpression''의 클래스 데프을 포함해야한다가 괜찮 았는데. – StaxMan

+0

나는 그것을 잊어 버렸다. 그냥했다. –

답변

3

보호 된 부분은 문제가되지 않아야합니다 (확인 됨). 그러나 생성자가 인수를 취할 수 있습니다. 사용할 기본이 아닌 생성자를 나타내려면 @JsonCreator; 그러나 그 이상으로 어떤 종류의 생성자 (또는 정적 팩토리 메소드)를 사용할 것인가에 달려있다.

세부 정보를 알고 싶으면 클래스 정의가 필요합니다. 또 다른 가능성은 비 정적 인 내부 클래스를 다루려고한다는 것입니다.

+0

예, 인수가 필요하지만 라이브러리에서 클래스 생성자에 액세스 할 수 없다면 주석을 달 수는 없습니까? 따라서 생성자에 주석을 달아야하는 객체를 재 구축하려면 잭슨이 인수가없는 생성자를 사용하려고 시도하지 않아도됩니까? –

+0

주석이없는 올바른 정보 Jackson은 ctor 매개 변수 이름이 바이트 코드에 저장되어 있지 않으므로 다른 ctors를 알아낼 수 없습니다. 그러나 믹서 인 어노테이션을 사용할 수 있습니다. (http://wiki.fasterxml.com/JacksonMixInAnnotations), 심지어 메소드 나 필드가 아닌 생성자를 위해. – StaxMan

+0

Wrt 믹스 인 어노테이션 : 다음으로 일치하는 생성자 (같은 순서로 동일한 인수 유형)로 더미 클래스를 정의한다. @JsonCreator'로 표시하고 각 매개 변수는 일치하는 JSON 이름을 지정하는'@JsonProperty'를 표시합니다. 일단 ObjectMapper에 대한 혼합 주석을 등록하면 작동합니다. – StaxMan

0

any1이 스레드에 오는 경우. 동일한 문제가있었습니다. 난 그냥 생성자

protected SimpleExpression(){} 

를 추가하고 그 뭔가가 무엇 이후

+0

최종 속성이 초기화되지 않았으므로 컴파일되지 않습니다. 명시 적으로 각 속성을 널 (NULL) 또는 기본값으로 설정하면 작동합니다. –