Apache CXF提供了用于方便地构建和开发WebService的可靠基础架构。它允许创建高性能和可扩展的服务,可以部署在Tomcat和基于spring的轻量级容器中

  1. Different Types Of Services SOAP - this page summarizes the options for creating SOAP services. REST-ful - REST support is described here. CORBA

  1. The JAX-WS APIs include a set of annotations which allow you to build services using annotated classes. These services are based on a single class which contains a set of operations.
    翻译过来不好看,JAX_WS包含一些API进行创建WS..
    @WebMethod:定义方法 llows you to customize the operation name, exclude the operation from inclusion in the service, etc
    @WebParam :定义参数,输入输出参数 allows you to customize a parameter’s name, namespace, direction (IN or OUT), etc
    @WebResult -:(我没有看懂,拥有了输入输出参数,还需要什么呢?) allows you to customize the return value of the web service call
  2. 发布服务的方法
    The JAX-WS standard Endpoint APIs
    CXF’s XML configuration format - i.e. < jaxws:endpoint … />
  3. 工具支持:CXF提供了在Java Bean、WebService和WSDL之间进行转换的工具,提供了对Maven和Ant集成的支持,并无缝地支持Spring集成

一个小小的例子

创建一个Maven工程

<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>
<!-- 如果CXF不集成到Web服务器中,必须添加该引用 -->
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>

​​

​​ ​​

To create a service starting from Java you need to do the following:

[Create a Service Endpoint Interface (SEI) that defines the methods you wish to expose as a service]暴露接口出来

[添加注解代码Add the required annotations to your code.]

[Publish the service.]

编写接口:

The SEI is a standard Java interface. It defines a set of methods that a class will implement. It can also define a number of member fields and constants to which the implementing class has access. 定义函数,有的有变量

In the case of an SEI the methods defined are intended to be mapped to operations exposed by a service(暴露接口). The SEI corresponds to a wsdl:portType element(这个接口对应一个portType). The methods defined by the SEI correspond to wsdl:operation elements in the wsdl:portType element.(整个方法呢,对应当前接口的操作)

定义的接口,小毛驴一个,慢慢的学,看着文档…..

package com.cxf.HelloDemo;

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

/**
* Created by JetWang on 2016/11/29.
*/

@WebService(name = "HelloWS ",
targetNamespace = "http://www.tmp.com/services/hello"
)
public interface HelloWS {
@WebMethod
String welcome(@WebParam(name = "name") String name);
}

如下就是我们定义之后产生的wsdl的部分的文件信息.. 刚刚对应WSDL的文件的信息字段

<wsdl:portType name="HelloWS ">
<wsdl:operation name="welcome">
<wsdl:input message="tns:welcome" name="welcome"></wsdl:input>
<wsdl:output message="tns:welcomeResponse" name="welcomeResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>

看看@WebService中的字段的信息

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface WebService {
String name() default "";
/**
Specifies the name of the service interface.
This property is mapped to the name attribute of the
in a WSDL contract. The default is to append
PortType to the name of the implementation class.
(默认的PortType wsdl中的为实现类的名称 可以不写)
**/
String targetNamespace() default "";
/**
Specifies the target namespace under which
specified, the target namespace is derived from the package name.
(这个属性就是当前的发布服务的命名的空间,有点内向jar包 ,可以不写)
**/
String serviceName() default "";
/**
Specifies the name of the published service.
This property is mapped to the name attribute of the
The default is to use the name of the service's
(发布服务的名称,不能定义在接口里面哦,默认名称为实现类 可以不写)
**/
String portName() default "";
/**
Specifies the name of the endpoint at which the
service is published. This property is mapped to the
name attribute of the wsdl:port element that specifies
The default is the append Port to the name of the service's
implementation class. Note: Not allowed on the SEI 。
(当服务被发布的时候,更加详细的描述当前服务的名称,使用wsdl:port 可以不写,默认类名称+port)
**/
String wsdlLocation() default "";
/**
Specifies the URI at which the service's
URI at which the service is deployed.
(保存wsdl的地址,默认为发布的地址)
**/
String endpointInterface() default "";
/**
Specifies the full name of the SEI that the implementation
attribute is used on a service implementation class.
Note: Not allowed on the SEI (实现类的名称包含包路径,不能使用在接口里面 必须写实现类中)
**/
}
你并不需要为任何的提供值@WebService标注的属性。但是,建议您提供尽可能多的信息,你可以。

注解服务的实现

package com.cxf.HelloDemo.impl;

import com.cxf.HelloDemo.HelloWS;

import javax.jws.WebService;

/**
* Created by JetWang on 2016/11/29.
*/
@WebService(
endpointInterface = "com.cxf.HelloDemo.HelloWS",//实现接口的地方
portName = "HelloWSPort",
serviceName = "HelloWSService",
targetNamespace = "http://www.tmp.com/services/hello"
)
public class HelloWSImpl implements HelloWS{

@Override
public String welcome(String name) {
return "Welcome " + name;
}
}

通过工厂发布,没有使用容器

public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloWS.class);
//服务发布的地址
factory.setAddress("http://localhost:8000/services/hello");
factory.setServiceBean(new HelloWSImpl());
factory.create();
}

​http://localhost:8000/services/hello?wsdl​​ 访问获取到的

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://www.tmp.com/services/hello" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWSService" targetNamespace="http://www.tmp.com/services/hello">//命名空间
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.tmp.com/services/hello" elementFormDefault="unqualified" targetNamespace="http://www.tmp.com/services/hello" version="1.0">
<xs:element name="welcome" type="tns:welcome"/>
<xs:element name="welcomeResponse" type="tns:welcomeResponse"/>
<xs:complexType name="welcome">
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="welcomeResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="welcome">
<wsdl:part element="tns:welcome" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="welcomeResponse">
<wsdl:part element="tns:welcomeResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloWS ">
<wsdl:operation name="welcome">
<wsdl:input message="tns:welcome" name="welcome"></wsdl:input>
<wsdl:output message="tns:welcomeResponse" name="welcomeResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWSServiceSoapBinding" type="tns:HelloWS ">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="welcome">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="welcome">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="welcomeResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWSService">
<wsdl:port binding="tns:HelloWSServiceSoapBinding" name="HelloWS Port">
<soap:address location="http://localhost:8000/services/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

分解来看看~~

1.wsdl:portType

<wsdl:portType name="HelloWS "> //发布服务的接口信息
<wsdl:operation name="welcome">//这个是方法
<wsdl:input message="tns:welcome" name="welcome"></wsdl:input>//这个是事务的信息 上面有详细的定义 有个方法 方法呢有参数...慢慢的看就懂了!
<wsdl:output message="tns:welcomeResponse" name="welcomeResponse"></wsdl:output>//这个是输出的信息
</wsdl:operation>
</wsdl:portType>
  1. wsdl:service 这个是实现的
<wsdl:service name="HelloWSService">//这个是定义好的吧
<wsdl:port binding="tns:HelloWSServiceSoapBinding" name="HelloWS Port">//这个是portName binding看前面
<soap:address location="http://localhost:8000/services/hello"/>
</wsdl:port>
</wsdl:service>

这些一般的都够用了…..

github地址: ​​https://github.com/WangJi92/CXFDemo/tree/CXFDemo1​