Apache-cxf-with-jaxws

提供:Dev Guides
移動先:案内検索

JAX-WSを使用したApache CXF

このJAX-WSアプリケーションでは、以前のPOJOアプリケーションのようなApache CXFファーストのアプローチを使用します。 そのため、最初にWebサービスのインターフェイスを作成します。

サービスインターフェイスの宣言

前のケースと同様に、グリーティングと呼ばれるインターフェースメソッドを1つだけ持つ簡単なサービスを作成します。 サービスインターフェイスのコードは以下に示されています-

//HelloWorld.java
package com.finddevguides.cxf.jaxws.helloworld;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
   String greetings(String text);
}

インターフェイスに @ WebService タグで注釈を付けます。 次に、このインターフェイスを実装します。

Webインターフェイスの実装

Webインターフェイスの実装はここに示されています-

//HelloWorldImpl.java
package com.finddevguides.cxf.jaxws.helloworld;
public class HelloWorldImpl implements HelloWorld {
   @Override
   public String greetings(String name) {
      return ("hi " + name);
   }
}

greetingsメソッドには @ Override タグで注釈が付けられています。 このメソッドは、呼び出し元に「hi」メッセージを返します。

次に、サーバーを開発するためのコードを記述します。

開発サーバー

POJOアプリケーションとは異なり、CXFが提供するEndpointクラスを使用してサービスを公開することにより、インターフェイスを分離します。 これは、次の2行のコードで行われます-

HelloWorld implementor = new HelloWorldImpl();
Endpoint.publish(
   "http://localhost:9090/HelloServerPort",
   implementor,
   new LoggingFeature()
);

publishメソッドの最初のパラメーターは、クライアントがサービスを利用できるURLを指定します。 2番目のパラメーターは、サービスの実装クラスを指定します。 サーバーのコード全体が以下に示されています-

//Server.java
package com.finddevguides.cxf.jaxws.helloworld;
import javax.xml.ws.Endpoint;
import org.apache.cxf.ext.logging.LoggingFeature;
public class Server {
   public static void main(String[] args) throws Exception {
      HelloWorld implementor = new HelloWorldImpl();
      Endpoint.publish("http://localhost:9090/HelloServerPort",
      implementor,
      new LoggingFeature());
      System.out.println("Server ready...");
      Thread.sleep(5 *60* 1000);
      System.out.println("Server exiting ...");
      System.exit(0);
   }
}

サーバーをデプロイするには、以下にリストされているように、プロジェクトにいくつかの変更を加える必要があります。

サーバーの展開

最後に、サーバーアプリケーションをデプロイするには、pom.xmlをもう1つ変更して、アプリケーションをWebアプリケーションとして設定する必要があります。 あなたが pom.xml に追加する必要があるコードは以下のとおりです-

<profiles>
   <profile>
      <id>server</id>
      <build>
         <defaultGoal>test</defaultGoal>
         <plugins>
            <plugin>
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>exec-maven-plugin</artifactId>
               <version>1.6.0</version>
               <executions>
                  <execution>
                     <phase>test</phase>
                     <goals>
                        <goal>java</goal>
                     </goals>
                     <configuration>
                        <mainClass>
                           com.finddevguides.cxf.jaxws.helloworld.Server
                        </mainClass>
                     </configuration>
                  </execution>
               </executions>
            </plugin>
         </plugins>
      </build>
   </profile>
</profiles>

アプリケーションをデプロイする前に、プロジェクトにさらに2つのファイルを追加する必要があります。 これらは、以下のスクリーンショットに示されています-

JAXWSアプリケーションをデプロイする前

これらのファイルは、 CXFServlet のマッピングを定義するCXF標準ファイルです。 web.xml ファイル内のコードは、クイックリファレンスのためにここに示されています-

//Web.xml
<?xml version = "1.0" encoding = "UTF-8"??>
<web-app xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   <display-name>cxf</display-name>
   <servlet>
      <description>Apache CXF Endpoint</description>
      <display-name>cxf</display-name>
      <servlet-name>cxf</servlet-name>
      <servlet-class>
         org.apache.cxf.transport.servlet.CXFServlet
      </servlet-class>
      <load-on-startup>
         1
      </load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>
         cxf
      </servlet-name>
      <url-pattern>
        /services/*
      </url-pattern>
   </servlet-mapping>
   <session-config>
      <session-timeout>60</session-timeout>
   </session-config>
</web-app>
  • cxf-servlet.xml、*で、サービスのエンドポイントのプロパティを宣言します。 これは以下のコードスニペットに示されています-
<beans ...>
   <jaxws:endpoint xmlns:helloworld = "http://finddevguides.com/"
      id = "helloHTTP"
      address = "http://localhost:9090/HelloServerPort"
      serviceName = "helloworld:HelloServiceService"
      endpointName = "helloworld:HelloServicePort">
   </jaxws:endpoint>
</beans>

ここで、サービスエンドポイントのID、サービスを利用できるアドレス、サービス名、エンドポイント名を定義します。 これで、CXFサーブレットによってサービスがどのようにルーティングおよび処理されるかを学習しました。

最終的なpom.xml

*pom.xml* には、さらにいくつかの依存関係が含まれています。 すべての依存関係を記述するのではなく、pom.xmlの最終バージョンを以下に含めました-
<?xml version = "1.0" encoding = "UTF-8"??>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.finddevguides</groupId>
   <artifactId>cxf-jaxws</artifactId>
   <version>1.0</version>
   <packaging>jar</packaging>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
   </properties>
   <profiles>
      <profile>
         <id>server</id>
         <build>
            <defaultGoal>test</defaultGoal>
            <plugins>
               <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>exec-maven-plugin</artifactId>
                  <version>1.6.0</version>
                  <executions>
                     <execution>
                        <phase>test</phase>
                        <goals>
                           <goal>java</goal>
                        </goals>
                        <configuration>
                           <mainClass>
                              com.finddevguides.cxf.jaxws.helloworld.Server
                           </mainClass>
                        </configuration>
                     </execution>
                  </executions>
               </plugin>
            </plugins>
         </build>
      </profile>
      <profile>
         <id>client</id>
         <build>
            <defaultGoal>test</defaultGoal>
            <plugins>
               <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>exec-maven-plugin</artifactId>
                  <executions>
                     <execution>
                        <phase>test</phase>
                        <goals>
                           <goal>java</goal>
                        <goals>
                        <configuration>
                           <mainClass>
                              com.finddevguides.cxf.jaxws.helloworld.Client
                           </mainClass>
                        </configuration>
                     </execution>
                  </executions>
               </plugin>
            </plugins>
         </build>
      </profile>
   </profiles>
   <dependencies>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-frontend-jaxws</artifactId>
         <version>3.3.0</version>
      </dependency>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-transports-http</artifactId>
         <version>3.3.0</version>
      </dependency>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-features-logging</artifactId>
         <version>3.3.0</version>
      </dependency>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-transports-http-jetty</artifactId>
         <version>3.3.0</version>
      </dependency>
   </dependencies>
</project>

このチュートリアルの後半のセクションで学習するクライアントを構築するためのプロファイルも含まれていることに注意してください。

HelloWorldサービスの実行

これで、Webアプリを実行する準備が整いました。 コマンドウィンドウで、次のコマンドを使用してビルドスクリプトを実行します。

mvn clean install
mvn -Pserver

コンソールに次のメッセージが表示されます-

INFO: Setting the server's publish address to be http://localhost:9090/HelloServerPort
Server ready…

前と同じように、ブラウザでサーバーURLを開いてサーバーをテストできます。

サーバーのURLを開く

操作を指定しなかったため、アプリケーションからエラーメッセージのみがブラウザに返されます。

ここで、*?wsdl *をURLに追加してみてください。次の出力が表示されます-

image

したがって、サーバーアプリケーションは期待どおりに実行されています。 前述の Postman などのSOAPクライアントを使用して、サービスをさらにテストできます。

次のセクションでは、サービスを使用するクライアントの作成方法を学習します。

クライアントの開発

CXFアプリケーションでクライアントを作成するのは、サーバーを作成するのと同じくらい簡単です。 クライアントの完全なコードは次のとおりです-

//Client.java
package com.finddevguides.cxf.jaxws.helloworld;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public final class Client {
   private static final QName SERVICE_NAME
   = new QName("http://helloworld.jaxws.cxf.finddevguides.com/",
   "HelloWorld");
   private static final QName PORT_NAME
   = new QName("http://helloworld.jaxws.cxf.finddevguides.com/",
   "HelloWorldPort");
   private Client() {
   }
   public static void main(String[] args) throws Exception {
      Service service = Service.create(SERVICE_NAME);
      System.out.println("service created");
      String endpointAddress = "http://localhost:9090/HelloServerPort";
      service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
      endpointAddress);
      HelloWorld hw = service.getPort(HelloWorld.class);
      System.out.println(hw.greetings("World"));
   }
}

ここでは、CXF提供の Service クラスを使用して、既知のサービスにバインドします。 Service クラスで create メソッドを呼び出して、サービスのインスタンスを取得します。 service インスタンスで addPort メソッドを呼び出して、既知のポートを設定します。

これで、サービスを使用する準備ができました。まず、 service インスタンスで getPort メソッドを呼び出してサービスインターフェイスを取得します。 最後に、 greetings メソッドを呼び出して、コンソールに挨拶メッセージを印刷します。

Apache CXF-Firstアプローチを使用してCXFの基本を学習したので、次の章でWSDL-FirstアプローチでCXFを使用する方法を学習します。