GithubHelp home page GithubHelp logo

jian-feng / fuse-soap-server-demo Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 14 KB

本サンプルでは、camel-cxfコンポーネントを使って、SOAP Webサービス(Server)の実装方法を示します。Webサービスの開発手法は、Coding-FirstとWSDL-Fisrtの2種類がありますが、 今回は、素早くWebサービスを提供する目的で、Coding-Firstの手法を採用しました。

License: Apache License 2.0

Java 100.00%

fuse-soap-server-demo's Introduction

Fuseサンプル - WebサービスのServer実装

ソフトウェアバージョン

  • Fuse 7.x (ローカル、またはOpenshift上)
  • Spring-boot 2.1
  • Java 1.8

本サンプルについて

本サンプルでは、camel-cxfコンポーネントを使って、SOAP Webサービス(Server)の実装方法を示します。Webサービスの開発手法は、Coding-FirstとWSDL-Fisrtの2種類がありますが、 今回は、素早くWebサービスを提供する目的で、Coding-Firstの手法を採用しました。

SOAP Clientの実装は、fuse-soap-client-demoを参照してください。

また、camel-cxfのオプションが非常に多いですが、今回は初心者向けのため、最低限の設定のみとなっています。他の詳細なオプションはリンク先の製品ドキュメントをご参照ください。

処理概要

1. cxfEndpoint: orderService

cxfEndpoint Beanを使って、orderService(Webサービスのインタフェース)をWebサービスに公開します。

<cxf:cxfEndpoint id="orderService"
  address="http://localhost:9090/order/"
  serviceClass="com.sample.OrderService"/>

上記のcom.sample.OrderServiceでは、Javaインタフェースを使ったWebサービスの定義となっています。

@WebService
public interface OrderService {
  String order(
    @WebParam(name = "product") String product,
    @WebParam(name = "amount") int amount
    );
}

これらの設定だけで、camel-cxfはJavaインタフェースの内容を元に、動的にWSDLを生成してくれます。

2. Camel Route: simple-route

このCamel Routeは、cxfEndpointからSOAPリクエストを受け取ります。 この時点で、既にSOAPメッセージのボディはPOJOに変換され、exchange.in.bodyにセットされます。

この後、myTransformerで3桁の乱数を生成してexchange.in.bodyにセットします。

最後は、cxfEndpointが自動的に、exchange.in.bodyをSOAPメッセージに変換してWebサービスクライアントへ返します。

<route id="simple-route">
  <from uri="cxf:bean:orderService" />
  <log message=">>> receive: ${body}" />
  <transform>
    <method ref="myTransformer"/>
  </transform>
  <log message=">>> send: ${body}"/>
</route>

本サンプルの実行方法

1. ローカルPCで実行する場合

前提条件:

  • Maven 3.x がローカルPCでインストール済みであること
  • Maven Remote Repositoryがアクセスできること

実行手順:

  1. コマンドプロンプトにて実行します。

    mvn clean spring-boot:run
  2. コマンドプロンプトでWebサービスが公開されることを確認してください。

    INFO  o.a.c.w.s.f.ReflectionServiceFactoryBean - Creating Service {http://sample.com/}OrderServiceService from class com.sample.OrderService
    INFO  org.apache.cxf.endpoint.ServerImpl - Setting the server's publish address to be http://localhost:9090/order/
    
  3. WebサービスのWSDLのを確認します。
    ブラウザ、若しくはSOAP UIで下記のURLへアクセスしてWSDLを確認できます。
    URL: http://localhost:9090/order?wsdl

  4. Webサービスを呼び出して見ます。
    Curl、若しくはSOAP UIで下記のエンドポイントへリクエストしてください。

    curl -d @soap-request.xml http://localhost:9090/order

    レスポンス例として、以下になります。"164"とは、CamelRouteが自動生成した3桁の乱数です。

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <ns2:orderResponse xmlns:ns2="http://sample.com/">
          <return>164</return>
        </ns2:orderResponse>
      </soap:Body>
    </soap:Envelope>
  5. コマンドプロンプトでCamel Routeのログを確認します。

    INFO  simple-route - >>> receive: abc,10
    INFO  simple-route - >>> send: 164

ゼロから作成の手順

以下は本サンプルをゼロから作成する場合の手順を説明します。

※ 本手順の実施には、インターネット接続が必要。

  1. Maven archetypeより、Fuseアプリの雛形を生成します。

    # Config archetype to use
    archetypeVersion=2.2.0.fuse-sb2-750011-redhat-00006
    archetypeCatalog=https://maven.repository.redhat.com/ga/io/fabric8/archetypes/archetypes-catalog/${archetypeVersion}/archetypes-catalog-${archetypeVersion}-archetype-catalog.xml
    
    # Config Fuse project name to generate
    MY_PROJECT=fuse-soap-server-demo
    
    # Generate from archetype, which is SpringBoot2 based Camel Application using XML DSL
    mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate -B \
      -DarchetypeCatalog=${archetypeCatalog} \
      -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \
      -DarchetypeVersion=${archetypeVersion} \
      -DarchetypeArtifactId=spring-boot-camel-xml-archetype \
      -DgroupId=com.sample \
      -DartifactId=${MY_PROJECT}  \
      -DoutputDirectory=${MY_PROJECT}
  2. 依存ライブラリcamel-cxfとcxf-runtime-transportを追加

    File: pom.xml

    <!-- Camel with CXF on Spring-boot -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-cxf-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-undertow</artifactId>
    </dependency>
  3. WebServiceインターフェイスを作成
    File: src/main/java/com/sample/OrderService.java

    @WebService
    public interface OrderService {
    
      String order(
        @WebParam(name = "product") String product,
        @WebParam(name = "amount") int amount
        );
    }

    WebServiceインターフェイスの細かいカスタマイズ方法は、以下を参照してください。

  4. camel-contextのXMLネームスペースを修正
    xmlns:cxfxsi:schemaLocationを以下のように追加します。
    File: src/main/resources/spring/camel-context.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cxf="http://camel.apache.org/schema/cxf"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
           http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd">
  5. camel-contextへcxfエンドポイントのbean定義を追加
    File: src/main/resources/spring/camel-context.xml

    <cxf:cxfEndpoint id="orderService"
      address="http://localhost:9090/order/"
      serviceClass="com.sample.OrderService"/>
  6. camel-context内のCamel Routeをすべて削除して、以下のRouteを追加
    File: src/main/resources/spring/camel-context.xml

    <route id="simple-route">
      <from uri="cxf:bean:orderService" />
      <log message=">>> receive: ${body}" />
      <transform>
        <method ref="myTransformer"/>
      </transform>
      <log message=">>> send: ${body}"/>
    </route>

fuse-soap-server-demo's People

Contributors

jian-feng avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.