2016-07-05 3 views
0

내가 객체에서 JSON 개체를 얻기 위해 노력하고 있어요를 변경하지 않고 순환 종속성을 제거하지만 제공 아래 인터넷에 검색 예외자바 - JSON이 - 엔티티

at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) 
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) 

및 솔루션 실패입니다 @JSONIgnore, @JSONBackReference 하지만 내 프로젝트는 너무 커서 기존 엔티티를 변경할 여력이 없습니다.

순환 의존성을 없애고 객체에서 적절한 json 문자열을 얻을 수있는 다른 방법이 있습니까?

+0

http://stackoverflow.com/questions/13764280/how-do-i-exclude-fields-with-jackson-not-using-annotations –

+0

감사합니다. Amit에게 감사 드려요.하지만 여기에도 특정 필드를 정의하거나 주석을 추가하고 있습니다. 나는 POJO 수업에 참여하고 싶지 않다. – monika

답변

0

나는 그것을 건너 뛸 것, 다시 같은 만남 여기

class MyExclusionStrategy implements ExclusionStrategy { 
Set<String> encounteredClasses = new HashSet<String>(); 

public boolean shouldSkipField(FieldAttributes fa) { 
    return false; 
} 

@Override 
public boolean shouldSkipClass(Class<?> type) { 
    System.out.println(encounteredClasses); 
    if (encounteredClasses.contains(type.getSimpleName().toString())) { 
     return true; 
    } if (type.isAnnotationPresent(Entity.class)) { 
     encounteredClasses.add(type.getSimpleName().toString()); 
    } 
    return false; 
}} 

가 세트에 이미 발생 클래스를 계속 추가 할 아래로 GSON ExclusionStrategy를 사용하고있는 경우 문제를 해결할 수 있습니다.

관련 문제