Cxf - Server

說明:利用Maven來建立cxf webservice方法。

pom檔中加入cxf+springmvc dependency

<!-- springmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.2.2.RELEASE</version>
</dependency>

<!-- cxf web service -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.6</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.6</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-databinding-aegis</artifactId>
    <version>3.1.6</version>
</dependency>

撰寫接口

package com.webservice.cxf;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class AddOperation {

    @WebMethod
    public double add(double a, double b) {
        return a + b;
    }

    @WebMethod
    public double sub(double a, double b) {
        return a-b;
    }

}

web.xml加入servlet

<!-- for axis2 web service 使用 -->
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-base.xml</param-value>
    </context-param>

      <!-- cxf web service -->
    <servlet>
        <servlet-name>CXFService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <!-- Spring MVC 的 routing servlet -->
    <servlet>
        <description>Spring MVC</description>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/application-base.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

spring設定檔 - application-base.xml

<?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:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://cxf.apache.org/jaxws   
                           http://cxf.apache.org/schemas/jaxws.xsd">

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

    <bean id="addOperation" class="com.webservice.cxf.AddOperation" />

    <jaxws:endpoint id="addOperationEndpoint" implementor="#addOperation"
        address="/addOperationService" />

</beans>

這樣就完成cxf的配置囉!

wsdl路徑:http://localhost:8080/cxfMaven/cxf/addOperationService?wsdl

results matching ""

    No results matching ""