Apache CXF使用Jetty發佈WebService

1、概述

Apache CXF提供了用於方便地構建和開發WebService的可靠基礎架構。它容許建立高性能和可擴展的服務,能夠部署在Tomcat和基於Spring的輕量級容器中,也能夠部署在更高級的服務器上,例如Jboss、WebSphere或WebLogic。 CXF提供瞭如下功能:前端

  • WebService服務標準支持:

Java API for XML Web Services (JAX-WS)
SOAP
WebService描述語言(Web Services Description Language ,WSDL)
消息傳輸優化機制(Message Transmission Optimization Mechanism,MTOM)
WS-Basic Profile
WS-Addressing
WS-Policy
WS-ReliableMessaging
WS-Securityapache

  • 前端建模:CXF容許使用不一樣的前端API來建立Service。如CXF容許使用簡單的工廠Bean並經過JAX-WS實現來建立WebService,容許建立動態WebService客戶端。
  • 工具支持:CXF提供了在Java Bean、WebService和WSDL之間進行轉換的工具,提供了對Maven和Ant集成的支持,並沒有縫地支持Spring集成。
  • RESTful支持:CXF支持Restful,並支持Java平臺的JAX-RS實現。
  • 對不一樣傳輸和綁定的支持:CXF支持不一樣數據類型的傳輸,除了支持SOAP和HTTP協議綁定外,還支持JAXB和AEGIS綁定。
  • 對非XML綁定的支持:CXF支持非XML綁定,如JSON、CORBA、JBI和SCA等。
  • Code First和Xml First:CXF支持使用Code First或者Xml First的方式建立WebService。

 

2、使用CXF內置jetty發佈WebService

maven:瀏覽器

<properties>  
        <cxf.version>3.1.4</cxf.version>  
</properties>

<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>  
          
          
<!-- 使用cxf內置的jetty服務器發佈WebService -->  
<dependency>  
        <groupId>org.apache.cxf</groupId>  
        <artifactId>cxf-rt-transports-http-jetty</artifactId>  
        <version>${cxf.version}</version>  
</dependency> 

 

---服務器

接口:架構

@WebService
public interface HelloService{

    public String helloCxf();

    public String hello(String name);public User getUser(int id);

    public void saveUser(User user);

}

 

實現:frontend

@WebService(serviceName = "helloService",
        endpointInterface = "cn.lg.ws.hellocxf.HelloService"
)
public class HelloServiceImpl implements HelloService{

    @Override
    public String helloCxf(){
        return "Hello CXF!";
    }

    @Override
    public String hello(String name)
    {
        return "Hello " + name;
    }

    @Override
    public User getUser(int id) {
        User u1 = new User();
        return u1;
    }

    @Override
    public void saveUser(User user) {
        System.out.println(user.toString());
    }

}

 

發佈:maven

public class PublishService{
    /**
     * 使用CXF的JaxWsServerFactoryBean發佈服務
     * @param
     */
    public static void main(String[] args) {
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
        factory.setServiceClass(HelloService.class);
        //服務發佈地址
        factory.setAddress("http://localhost:8088/soap/hello");
        factory.setServiceBean(new HelloServiceImpl());
        factory.create();
        System.out.println("publish success");
    }
}

 

使用瀏覽器訪問 http://localhost:8088/soap/hello?wsdl 能夠看到wsdl以下,則說明發布成功ide

 

使用CXF在客戶端調用WebService:工具

public class ClientTest{
    public static void main(String[] args) {
        JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();
        jwpfb.setServiceClass(HelloService.class);
        jwpfb.setAddress("http://localhost:8088/sop/hello");
        HelloService hs = (HelloService) jwpfb.create();
        System.out.println(hs.getUser(101));
        Q.p(hs.hello("luangeng"));
    }
}

---性能

相關類可經過如下命令產生:

wsimport -p com.ickes.cxf.client -keep http://localhost:8088/sop/hello?wsdl

相關文章
相關標籤/搜索