久しぶりにJavaしている今日この頃です(5)

採用するかどうかはともかく、Apache CXF wit Spring 2.5でWebサービスを試してみました(・∀・)


まずはWebサービスの実装クラスを適当に(・ω・)
Springで自動登録するために、@Serviceアノテーションも付加。

@Service
@WebService
public class SampleWebService
{
    @Autowired
    private GenericDao genericDao;

    public String sayHello(@WebParam(name="name") String name)
    {
        return "Hello, " + name;
    }

    public Collection<Data> getDataList()
    {
        ...
    }
}

次にweb.xmlの設定で、CXFServletを追加。

<servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/webservice/*</url-pattern>
</servlet-mapping>

そんでSpringのapplicationContext.xmlについて。
用途毎に分割しているんで、基本の部分とWebサービス用のファイルをこんな感じで。

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
                           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <!-- 自動登録 -->

    <context:annotation-config />
    <context:mbean-export/>
    <context:component-scan base-package="jp.xxxxx" />

...

</beans>
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cxf="http://cxf.apache.org/core"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
                           http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <!-- CXF -->

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <!-- Service Factory -->

    <bean id="wsServiceFactory" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
        <property name="dataBinding">
            <bean class="org.apache.cxf.aegis.databinding.AegisDatabinding"/>
        </property>
        <property name="serviceConfigurations">
            <list>
               <bean class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration"/>
               <bean class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration"/>
               <bean class="org.apache.cxf.service.factory.DefaultServiceConfiguration"/>
           </list>
       </property>
    </bean>

    <!-- Web Service -->

    <jaxws:endpoint id="sampleEndPoint" implementor="#sampleWebService" implementorClass="jp.xxxxx.webservice.SampleWebService"
                    address="/Sample">
        <jaxws:serviceFactory>
            <ref bean="wsServiceFactory"/>
        </jaxws:serviceFactory>
    </jaxws:endpoint>

</beans>

テスト用のクライアントは.NETで作成。
サーバーを実行させて、下記URLからプロキシを自動生成。

http://127.0.0.1:8080/app/webservice/Sample?wsdl

こんな感じで.NET-JavaInteropも確認できまスタ。

SampleWebServiceClient client = new SampleWebServiceClient();
Debug.WriteLine( client.sayHello( "うさうさ" ) );

Data[] list = client.getDataList();

で、ちょっとだけ注意点(・∀・)
CXF 2.0.4だとSpring 2.5との連携に問題があったので、CXF 2.0.5のsnapshotを使用。
後、CXFのXMLスキーマhttp://cxf.apache.org/...には置いてないので、XMLエディタ上で怒られたりしますが、そこはCXFのjarを解凍してその中のファイルを参照するようにしました。


っと.NETとの相互連携も確認していたら、.NET側システムのアーキテクチャ設計もやってくれない?、とか言われるし( ゚д゚)
5月以降なら空いてくるだろうけど、むしろ.NET側システムのリリースの方が先だし、ありえんw