Apache CXF實現Web Service(1)——不借助重量級Web容器和Spring實現一個純的JAX-WS web service

Richaaaard發表於2015-12-02

廢話少說,先在Eclipse中新建一個Java Project

(可以不是WTP的Dynamic Web Project)

 

選擇Java Project

再看pom.xml

我們使用cxf 3.1.4版本,

 

<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/maven-v4_0_0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <groupId>com.cnblog.richaaaard.cxfstudy</groupId>  
    <artifactId>cxf-test-standalone-ws-helloworld</artifactId>  
    <packaging>war</packaging>  
    <version>1.0-SNAPSHOT</version>  
    <name>cxf-test-standalone-ws-helloworld Maven Webapp</name>  
    <url>http://maven.apache.org</url>  
      
    <properties>  
<!-- <cxf.version>2.7.18</cxf.version> -->
<!-- 注意使用2.x.x版本的同學,下面例子中引入cxf-bundle-jaxrs是不必要的,因為jaxws啟動一個服務需要類JaxWsServerFactoryBean在cxf-rt-frontend-jaxws.jar中-->
        <cxf.version>3.1.4</cxf.version>        
    </properties>  
      
    <dependencies>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-frontend-jaxws</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-transports-http</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-transports-http-jetty</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-ws-security</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-ws-policy</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
<!--         <dependency>   -->
<!--             <groupId>org.apache.cxf</groupId>   -->
<!--             <artifactId>cxf-bundle-jaxrs</artifactId>   -->
<!--             <version>${cxf.version}</version>   -->
<!--         </dependency>   -->
        <dependency>  
            <groupId>javax.ws.rs</groupId>  
            <artifactId>jsr311-api</artifactId>  
            <version>1.1.1</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-api</artifactId>  
            <version>1.5.8</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-simple</artifactId>  
            <version>1.5.8</version>  
        </dependency>  
        <dependency>  
            <groupId>commons-httpclient</groupId>  
            <artifactId>commons-httpclient</artifactId>  
            <version>3.0</version>  
        </dependency>  
        <dependency>  
            <groupId>commons-io</groupId>  
            <artifactId>commons-io</artifactId>  
            <version>2.3</version>  
        </dependency>  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.8.1</version>  
            <scope>test</scope>  
        </dependency>  
    </dependencies>  
      
    <build>  
        <finalName>cxfstudy</finalName>  
        <resources>  
            <resource>  
                <directory>src/main/resources</directory>  
            </resource>  
            <resource>  
                <directory>src/main/java</directory>  
                <includes>  
                    <include>**</include>  
                </includes>  
                <excludes>  
                    <exclude>**/*.java</exclude>  
                </excludes>  
            </resource>  
        </resources>  
        <plugins>  
            <plugin>  
                <groupId>org.mortbay.jetty</groupId>  
                <artifactId>maven-jetty-plugin</artifactId>  
                <configuration>  
                    <contextPath>/</contextPath>  
                    <connectors>  
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
                            <port>9000</port>  
                        </connector>  
                    </connectors>  
                </configuration>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <configuration>  
                    <source>1.7</source>  
                    <target>1.7</target>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>  

另外有例子給出的slf4j是slf4j-jdk14 (http://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14)的版本,如果用jdk5以上的同學可以將其替換成slf4j-simple實現。

CXF Web Service (Annotation)實現,上程式碼

CXF支援兩種方式釋出一個web service,一種是Java Annotation(Bottom up),另外一種是先定義好wsdl和xsd schema然後通過工具生成(Top down),我們這裡的例子先介紹Java Annotation。

首先我們需要定義一個web service介面類HelloWorld,並加上Annotation

package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService  
public interface HelloWorld {  
    @WebMethod  
    @WebResult String sayHi(@WebParam String text);  
}  

在此我們先不介紹 @WebService、 @WebMethod、@WebResult還有@WebParam如何工作的。

然後我們需要用HelloWorldImpl來實現這個介面類,

package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services;

public class HelloWorldImpl implements HelloWorld {  
	public String sayHi(String name) {  
        String msg = "Hello " + name + "!";  
        return msg;  
    }  
}

釋出一個Web Service

我們先用一個最簡單的方式,使用javax.xml.ws.Endpoint來發布

package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.server;

import javax.xml.ws.Endpoint;

import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorldImpl;

public class SimpleServer {  
	
    public static void main(String[] args) throws Exception {
        
        System.out.println("Starting Server");
    	HelloWorldImpl implementor = new HelloWorldImpl();
    	String address = "http://localhost:9000/ws/HelloWorld";
    	Endpoint.publish(address, implementor);
    }  
}

注意:這裡的address路徑是大小寫敏感的,如果釋出成"http://localhost:9000/ws/helloWorld",而客戶端使用/HelloWorld會找不到服務。

直接Run As..-> Java Application

  

我們可以在Eclipse中看到一個jetty服務正常執行

Starting Server
128 [main] INFO org.apache.cxf.service.factory.ReflectionServiceFactoryBean - Creating Service {http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/}HelloWorldImplService from class com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld
487 [main] INFO org.apache.cxf.endpoint.ServerImpl - Setting the server's publish address to be http://localhost:9000/ws/HelloWorld
506 [main] INFO org.eclipse.jetty.server.Server - jetty-8.1.15.v20140411
554 [main] INFO org.eclipse.jetty.server.AbstractConnector - Started SelectChannelConnector@localhost:9000

此時,我們通過瀏覽器訪問http://localhost:9000/ws/HelloWorld?WSDL

如何訪問我們釋出的這個服務呢?

我們利用jaxws中的JaxWsProxyFactoryBean來消費這個服務

package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.client;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld;

public class Client {  
    public static void main(String[] args) {  
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.setServiceClass(HelloWorld.class);  
        factory.setAddress("http://localhost:9000/ws/HelloWorld");  
        HelloWorld helloworld = (HelloWorld) factory.create();  
        String reply = helloworld.sayHi("HI");
        System.out.println("Server said: " + reply);
        System.exit(0);
    }  
} 

  同樣右鍵選擇Client.java,Run As.. ->Java Application,我們可以看到執行後臺執行的日誌最後成功返回“Server said: Hello HI!”

114 [main] INFO org.apache.cxf.service.factory.ReflectionServiceFactoryBean - Creating Service {http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/}HelloWorldService from class com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld
531 [main] INFO org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld - Outbound Message
---------------------------
ID: 1
Address: http://localhost:9000/ws/HelloWorld
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHi xmlns:ns2="http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/"><arg0>HI</arg0></ns2:sayHi></soap:Body></soap:Envelope>
--------------------------------------
748 [main] INFO org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld - Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[258], content-type=[text/xml;charset=UTF-8], Server=[Jetty(8.1.15.v20140411)]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHiResponse xmlns:ns2="http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/"><return>Hello HI!</return></ns2:sayHiResponse></soap:Body></soap:Envelope>
--------------------------------------
Server said: Hello HI!

  上面Outbound Message和Inbound Message是我們加的兩個Logging Interceptor輸出的日誌,關於Interceptor的原理和更多使用方式,會專題介紹。

這樣就完成了一個Web Service的釋出和消費

 

*服務端的擴充套件

除了用一個最簡單的javax.xml.ws.Endpoint來發布CXF web service,我們還可以使用cxf-rt-frontend-jaxws.jar中的JaxWsServerFactoryBean來釋出一個web service,它可以讓我們更多的控制web service的行為,比如說加Logging Interceptor之類

package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.server;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld;
import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorldImpl;

public class Server {  
	
    public static void main(String[] args) throws Exception {  
    	JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
        factory.setServiceClass(HelloWorld.class);  
        HelloWorldImpl implementor = new HelloWorldImpl();
        factory.setServiceBean(implementor);
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.setAddress("http://localhost:9000/ws/HelloWorld");  
        factory.create();  
  
        System.out.println("Server start...");  
        Thread.sleep(60 * 1000);  
        System.out.println("Server exit...");  
        System.exit(0);
    }  
}

  

 

 

參考:

http://blog.csdn.net/kongxx/article/details/7525476

https://cwiki.apache.org/confluence/display/CXF20DOC/A+simple+JAX-WS+service

 

相關文章