2014-03-25 7 views
0

I 다음 XML 엔티티가 :ClassCastException이 비 정렬 화 JSON

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement 
public class DocumentDossier { 

    private XdsJsonSubmissionSet submissionSet; 
    private XdsJsonDocument documentEntry; 
    private List<XdsJsonFolder> folder; 
    private List<XdsJsonAssociation> association; 
    ... 

그리고 나는 다음과 같은 방식으로 JSON 파일 비 직렬화하기 위해 노력하고있어 : 나는 비 직렬화 할 필요가

String content = IOUtils.toString(is); 
    JSONObject obj = new JSONObject(content); 
    Configuration config = new Configuration(); 
    MappedNamespaceConvention con = new MappedNamespaceConvention(config); 
    XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con); 

    JAXBContext jc = JAXBContext.newInstance(DocumentDossier.class); 
    Unmarshaller unmarshaller = jc.createUnmarshaller(); 
    DocumentDossier result = (DocumentDossier) unmarshaller.unmarshal(xmlStreamReader); 

하지만 JSON을 루트 노드가 없으므로 루트 노드입니다. 여기에 JSON은 문제의 :

{ 
    "submissionSet": {...}, 
    "documentEntry": {...}, 
    "association": {...} 
} 

이 코드를 사용하여, 나는 다음과 같은 오류 얻을 :

java.lang.ClassCastException: XdsJsonSubmissionSet cannot be cast to DocumentDossier 

이 문제점은 무엇입니까?

+1

귀하의 질문에 이해가되지 않습니다 : 묵시적 루트 노드가 있습니다 (외부 '{}'는 사실 JSON "객체"입니다) 당신은 그 요소를'DocumentDossier'의 필드에 매핑해야합니다. 그래서 이것은 이미 작동해야합니다. 오류가 있습니까? 또는 구체적으로 건너 뛰고 싶은 점은 무엇입니까? –

+0

나는 다음과 같은 오류가 발생합니다 : XdsJsonSubmissionSet이 DocumentDossier 캐스트 할 수없는 내가 내 JSON 개체가 { "documentDossier"...}에 싸여 데 건너 뛰려면 : java.lang.ClassCastException가, 그래서 내 웹 서비스 것 내가 게시 한 것과 같은 JSON 객체를받을 수 있어야합니다. – user2451415

+0

JSON을'{ "documentDossier": ...} "로 감싸는 경우, JSON을 구문 분석 할 수 있으려면'DocumentDossier'를 래핑하는 다른 Java 클래스가 필요합니다. 위의 JSON은 이미'DocumentDossier'에 매핑되어 있어야합니다. 오류에는 다른 이유가 있어야합니다. –

답변

0

각 입력란에 @XmlElement을 추가해야한다고 생각합니다.

또한 @XmlRootElementXdsJsonSubmissionSet에, @XmlElement을 해당 입력란에 추가하는 것을 잊지 마십시오.

+0

이것은 나를 위해 문제를 해결하지 못했습니다. 대신 JSON을 InputStream으로 코드에 전달하고 "documentDossier"에 포장하기로 결정했습니다. {...}. 어쨌든 고마워! – user2451415

+0

이 포장은 절대 불필요합니다. 'DocumentDossier'를 마샬링 할 때 어떤 결과물을 얻으려고 했습니까? –