2012-03-26 11 views
1

Javaオブジェクト、つまりDTOにマップする必要があるXMLがあります。私のXMLは、私のXMLは、このJaxbとXMLPathを使用してXMLをアンマーシャリングできません

<UseCaseView> 
<FindCandidates> 
    <CandidatesRequest> 
     <APIRequest> 
      <Code>Code</Code> 
     </APIRequest> 
    </CandidatesRequest> 
</FindCandidates> </UseCaseView> 

「FindCandidates」のようなものを見て、「CandidatesRequestは」単なるラッパー要素と「APIRequest」です再び..です私のDTO内の任意のJavaオブジェクトを持っていないいくつかのラッパー要素を持っていますDTO ..

私は私のDTOにこのようXMLPathを使用しています...私のDTOはこのようになります。..

@XmlRootElement(name = "UseCaseView") 
public class FindRequestDTO implements Serializable{ 

private static final long serialVersionUID = 5528726225975606325L; 

private ApiRequestDTO apiRequest; 


@XmlPath("FindCandidates/CandidatesRequest/APIRequest") 
public ApiRequestDTO getAPIRequest() { 
    return apiRequest; 
    ......... 

私は2人のラッパーを削除する場合、これは、私のApiRequestDTOにAPIRequest要素をマッピングされていません要素と は直接XMLElementを使用してマップします名前=「APIRequest」)は、それが動作します...しかし、私はJaxb.propertiesは私のリソースフォルダ内の

"javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory" 

でファイルを追加した .. 2つのラッパー要素を無視し、私のDTOを構築する必要があります。

誰かが間違ってここに行くいただきました!私が知るのに役立ちます。..

おかげで、

+0

を返された実装クラスは何ですか?次の例は役立ちます:https://github.com/bdoughan/blog20110908。私は明日答えを投稿します。 –

+0

@Blaise - クイック返信用のthx ..返される実装クラスはcom.sun.xml.bind.v2.runtime.JAXBContextImpl – sampath

+0

あなたのクラスパスにeclipselink.jarがありますか?前のコメントでリンクした例は、Mavenを使ってコンパイルして実行するための設定です。 –

答えて

2

注:私はEclipseLink JAXB (MOXy)リードとJAXB 2 (JSR-222)専門家グループのメンバーです。

以下は、役立つ完全な例です。あなたのドメインモデルと同じパッケージに次のエントリでjaxb.propertiesと呼ばれるファイルを追加する必要があり、あなたのJAXBプロバイダとしてMOXYを指定するには

jaxb.properties

javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory 

FindRequestDTO

package forum9881188; 

import java.io.*; 
import javax.xml.bind.annotation.*; 
import org.eclipse.persistence.oxm.annotations.*; 

@XmlRootElement(name = "UseCaseView") 
public class FindRequestDTO implements Serializable { 

    private static final long serialVersionUID = 5528726225975606325L; 

    private ApiRequestDTO apiRequest; 

    @XmlPath("FindCandidates/CandidatesRequest/APIRequest") 
    public ApiRequestDTO getAPIRequest() { 
     return apiRequest; 
    } 

    public void setAPIRequest(ApiRequestDTO apiRequest) { 
     this.apiRequest = apiRequest; 
    } 

} 

ApiRequestDTO

package forum9881188; 

public class ApiRequestDTO { 
} 

デモ

package forum9881188; 

import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(FindRequestDTO.class); 

     FindRequestDTO fr = new FindRequestDTO(); 
     fr.setAPIRequest(new ApiRequestDTO()); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(fr, System.out); 
    } 

} 

出力

<?xml version="1.0" encoding="UTF-8"?> 
<UseCaseView> 
    <FindCandidates> 
     <CandidatesRequest> 
     <APIRequest/> 
     </CandidatesRequest> 
    </FindCandidates> 
</UseCaseView> 

詳細情報


UPDATE

何らかの理由であなたがJAXBContextのMOXYの実装を取得できない場合、あなたは常にブートストラップするネイティブAPIを使用することができます。あなたがjaxb.propertiesファイルは必要ありませんネイティブAPI使用時:あなたは `JAXBContext`を作成するとき

package forum9881188; 

import javax.xml.bind.*; 
import org.eclipse.persistence.jaxb.JAXBContextFactory; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     //JAXBContext jc = JAXBContext.newInstance(FindRequestDTO.class); 
     JAXBContext jc = JAXBContextFactory.createContext(new Class[] {FindRequestDTO.class}, null); 

     FindRequestDTO fr = new FindRequestDTO(); 
     fr.setAPIRequest(new ApiRequestDTO()); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(fr, System.out); 
    } 

} 
+0

私は、 Groovy.lang.MissingMethodException:メソッドのシグネチャがありません。静的なorg.eclipse.persistence.jaxb.JAXBContextFactory.createContext()は、引数の型に適用できます。([Ljava.lang.Object、null)values:[[クラス[CommonSvcRs]、null] 可能な解決策:createContext([Ljava.lang.Class]、java.util.Map)、createContext(java.lang.String、java.lang.ClassLoader)、createContext([Ljava.lang.Class ;、java.util.Map、java.lang.ClassLoader)、createContext([Ljava.lang.reflect.Type; java.util.Map、java.lang.ClassLoader)、... – Norm212

関連する問題