CXF--入門

BtWangZhi發表於2017-10-27

1 CXF是一個WebService框架。
HelloService.class

@WebService
public interface HelloService {

    public String say(String str);
}

HelloServiceImpl.class

@WebService
public class HelloServiceImpl implements HelloService{
    public String say(String str) {
        return "Hello"+str;
    }
}

1.1 採用jdk原生的程式碼註冊服務:

public static void main(String[] args){
        System.out.println("web Server Start");
        HelloService helloService=new HelloServiceImpl();
        String address="http://192.168.199.146:8080/helloWorld";
        Endpoint.publish(address, helloService);//jdk實現,暴露Service介面
        System.out.println("web Server end");
    }

可能會出現埠被佔用的情況,netstat -ano檢視埠對應的pid後在工作管理員中殺掉即可。
1.2 採用cxf WebService框架
核心架包:

<dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.5</version>
        </dependency>

        <!-- cxf核心包 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.5</version>
        </dependency>

        <!-- jetty -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.5</version>
        </dependency>
    </dependencies>
public static void main(String[] args){
        System.out.println("web Server Start");
        HelloService helloService=new HelloServiceImpl();
        String address="http://192.168.199.146:8080/helloWorld";
        JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
        factoryBean.setAddress(address);//設定暴露地址
        factoryBean.setServiceClass(HelloService.class);//設定介面類
        factoryBean.setServiceBean(helloService);//設定實現類
        factoryBean.create();//建立webService介面
        System.out.println("web Server end");
    }

http://192.168.199.146:8080/helloWorld?wsdl
這裡寫圖片描述
摘自:http://blog.java1234.com/blog/articles/52.html