web-dev-qa-db-ja.com

Webサービスクライアントを生成するのに最適なmavenのプラグインはどれですか?

WSクライアントを生成する必要がありますが、使用するプラグインを決定できません。これまでの私のオプションは、jaxb2-maven-plugin、axistools-maven-plugin、jaxws-maven-pluginです。

24
Neuquino

WSクライアントを生成する必要がありますが、使用するプラグインを決定できません。これまでの私のオプションは、jaxb2-maven-plugin、axistools-maven-plugin、jaxws-maven-pluginです。

まず、jaxb2-maven-pluginは、実際にはWSクライアントを生成するためのものではありません。排除された。

第二に、個人的に クライアント開発のみでもAxisを使用しない ので、axistools-maven-plugin。排除された。

これにより、JAX-WS RIとApache CXFスタック、およびそれぞれのMavenプラグイン JAX-WS Mavenプラグイン (JAX-WS Mavenプラグインを使用する手順 Usage ページ)と cxf-codegen-plugin にあります。

長所と短所については、次のようにまとめます。

最後に、どちらの選択肢もまともなので、リンクを少し見て、自分の意見を述べることをお勧めします。

37
Pascal Thivent

私はjaxws-maven-pluginを使用します。私の意見では、JAX-WSはWSの事実上の標準実装です。生成されたコードはAXISよりもはるかに優れており、構成と実装が簡単です。 MavenとSpringをサポートしています。

Wsdlファイルからpom.xmlのクライアント側コードを生成します。

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>generate-reports-ws-code</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                    <configuration>

<!-- This property is used to support having multiple <execution> elements. The plugin has, from some reason, only one timestamp file per the all executions, thus if you have two executions, it doesn't know exactly when to recompile the code. Here we tell it explicitly to have one timestamp file per each execution -->                            <staleFile>${project.build.directory}/jaxws/stale/.staleFlag.reports</staleFile>
                        <packageName>com.acme.reports.ws.api</packageName>
                        <wsdlDirectory>${project.build.directory}/wsdl</wsdlDirectory>
                        <wsdlFiles>
                            <wsdlFile>InternalReportsAPIService.wsdl</wsdlFile>
                        </wsdlFiles>
                        <verbose>true</verbose>
                        <sourceDestDir>${wsdl.generated.source.files.dir}</sourceDestDir>
                    </configuration>
                </execution>
            </executions>
        </plugin>

クライアントサービスBeanを作成するためのインターフェース(これは自動生成されません):

public interface InternalReportsAPIServiceFactory {

    public InternalReportsAPIService createInternalReportsAPIService();

}

そのBean実装:

public class InternalReportsAPIServiceFactoryBean implements InternalReportsAPIServiceFactory {

    private URL acmeReportsWsdlURL;

    private final static QName V1_QNAME = new QName("http://internal.reports.api.acme.net/v1","InternalReportsAPIService");

    @Override
    public InternalReportsAPIService createInternalReportsAPIService() {
        return new InternalReportsAPIService(acmeReportsWsdlURL, V1_QNAME);
    }

    public void setAcmeReportsWsdlUrl(String acmeReportsWsdlUrl) {
        try {
            this.acmeReportsWsdlURL = new URL(acmeReportsWsdlUrl);
        } catch (MalformedURLException ex) {
            throw new RuntimeException("Acme Reports WSDL URL is bad: "+ex.getMessage(), ex);
        }
    }
}

このBean(Spring Beanとして使用)のアイデアは、クライアントサービスコードを生成するためのシングルトンを用意することです。 2つの入力が必要です。WSDLurl-つまり、WSDLを実装するサーバーの実際のURL。クライアントサービスコードは、構築時に、指定されたURLでWSDLのget要求を送信します。次に、自動生成されたコードにある注釈に基づいてWSDLを作成し、それを比較します。これは、正しいサーバーバージョンに対して実行していることを確認するために行われたと思います。したがって、アプリケーションにアクセスできるプロパティファイルにURLを配置したので、Springアプリケーションのコンテキストファイルで初期化します。

ファクトリを使用してサービスを生成し、それを使用する例を次に示します。

InternalReportsAPIService internalReportsAPIService = acmeReportsWSFactory.createInternalReportsAPIService();
InternalReportsAPI port = internalReportsAPIService.getInternalReportsAPIPort();

ここから、ポート変数を使用して、wsdlで使用可能な操作を呼び出します。

5
Asaf Mesika

必要なMavenプラグイン:

  • cxf-Java2ws-plugin(JAX-WSからWSDL)
  • cxf-codegen-plugin(WSDLからJava)

JAX-WSからWSDLへcxf-Java2ws-pluginを「Java2ws」ゴールで構成することにより、JAX-WS注釈付きクラスからWSDLドキュメントを生成します。

プラグインの依存関係として、JAX-WS注釈付きクラスに必要なcxf-rt-frontend-jaxws依存関係とプロジェクトの依存関係を追加します。

<plugin>
   <groupId>org.Apache.cxf</groupId>
   <artifactId>cxf-Java2ws-plugin</artifactId>
   <version>2.5.1</version>
   <dependencies>
      <dependency>
         <groupId>org.Apache.cxf</groupId>
         <artifactId>cxf-rt-frontend-jaxws</artifactId>
         <version>2.5.1</version>
      </dependency>
      <dependency>
         <groupId>com.medici.app</groupId>
         <artifactId>services</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
   </dependencies>
   <executions>
      <execution>
         <id>generate-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <className>com.medici.app.services.WebServiceBean</className>
            <genWsdl>true</genWsdl>
         </configuration>
         <goals>
            <goal>Java2ws</goal>
         </goals>
      </execution>
   </executions>
</plugin>

WSDL 2 Java「wsdl2Java」ゴールでcxf-codegen-pluginを設定することにより、WSDLドキュメントからJavaクライアントを生成するため。

‘-p’引数はパッケージクラスを指定します。

生成されたクラスは、target/generated-sources/cxfフォルダーに配置されます。

<plugin>
   <groupId>org.Apache.cxf</groupId>
   <artifactId>cxf-codegen-plugin</artifactId>
   <version>2.5.1</version>
   <executions>
      <execution>
         <id>process-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <wsdlOptions>
               <wsdlOption>
                  <wsdl>${project.build.directory}/wsdl/WebService.wsdl</wsdl>
                  <extraargs>
                     <extraarg>-p</extraarg>
                     <extraarg>com.medici.app.client.model</extraarg>
                  </extraargs>
               </wsdlOption>
            </wsdlOptions>
         </configuration>
         <goals>
            <goal>wsdl2Java</goal>
         </goals>
      </execution>
   </executions>
</plugin>
0
Tiago Medici