您的位置:首页 > 移动开发

使用cxf发布webservice(JavaApplication)

2014-03-19 18:16 417 查看
第一步:下载CXF:http://www.apache.org/dyn/closer.cgi?path=/cxf/2.7.7/apache-cxf-2.7.7.zip 。解压后,新建一个Java项目,导入cxf中lib文件夹的所有jar包。

  第二步:编写接口CalculatorService.java,这个接口就是提供web服务的那个接口啦,里面的方法都是提供给人家调用滴。



package com.likunjie;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface CalculatorService {int add(int a, int b);
String concat(String a, String b);
}




  第三步:编写接口的实现类CalculatorServiceImpl.java,类里面要实现接口的方法。



package com.likunjie;
import javax.jws.WebService;

@WebService(endpointInterface="com.demo.CalculatorService",serviceName="calculator")
public class CalculatorServiceImpl implements CalculatorService {
@Override
public int add(int a, int b) {
return a + b;
}
@Override
public String concat(String a, String b) {
return a + b;
}
}




  第四步:开发服务端WebService.java,写完以后右击,运行Run As Java application(之后也不要关掉)。如果没有错误就继续下一步,有错误检查上一个类注解的地址是否跟你包名一致。检查是否有拼写错误。



package com.likunjie;

import javax.xml.ws.Endpoint;

public class WebService {
public static void main(String[] args) {
System.out.println("web service start");
CalculatorServiceImpl implementor = new CalculatorServiceImpl();
String address = "http://localhost:8080/calculator";  //这是上面在注解中已经声明过的URL
Endpoint.publish(address, implementor);
System.out.println("web service started");
}
}




  第五步:打开浏览器,输入:http://localhost:8080/calculator?wsdl ,如果显示以下信息,那么你已经完成了一大半。这个就是传说中的wsdl,要使用服务的客户端要先获取这个wsdl,知道里面描述了有什么服务使用以后。再根据这个wsdl,可以生成自己的客户端,并且访问所描述的服务。(这个一个schema文件,不懂的话再要去学一下)



<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://likunjie.com/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http"
name="calculator" targetNamespace="http://likunjie.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://likunjie.com/" elementFormDefault="unqualified"
targetNamespace="http://likunjie.com/" version="1.0">
<xs:element name="add" type="tns:add" />
<xs:element name="addResponse" type="tns:addResponse" />
<xs:element name="concat" type="tns:concat" />
<xs:element name="concatResponse" type="tns:concatResponse" />
<xs:complexType name="concat">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string" />
<xs:element minOccurs="0" name="arg1" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="concatResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="add">
<xs:sequence>
<xs:element name="arg0" type="xs:int" />
<xs:element name="arg1" type="xs:int" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence>
<xs:element name="return" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="concatResponse">
<wsdl:part element="tns:concatResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part element="tns:addResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="add">
<wsdl:part element="tns:add" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="concat">
<wsdl:part element="tns:concat" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="CalculatorService">
<wsdl:operation name="concat">
<wsdl:input message="tns:concat" name="concat"></wsdl:input>
<wsdl:output message="tns:concatResponse" name="concatResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="add">
<wsdl:input message="tns:add" name="add"></wsdl:input>
<wsdl:output message="tns:addResponse" name="addResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="calculatorSoapBinding" type="tns:CalculatorService">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="concat">
<soap:operation soapAction="" style="document" />
<wsdl:input name="concat">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="concatResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="add">
<soap:operation soapAction="" style="document" />
<wsdl:input name="add">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="addResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="calculator">
<wsdl:port binding="tns:calculatorSoapBinding" name="CalculatorServiceImplPort">
<soap:address location="http://localhost:8080/calculator" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>




第六步:客户端是完全不知道服务端以上的代码的。只有cxf和被提供的这个wsdl,那么怎样才能调用服务呢?CXF包的bin目录下其实有一个指令,叫wsdl2java,我们可以使用这个命令,根据得到的wsdl来生成java代码。接下来,小心跟着下面的步骤:1、打开命令行,路径切换到cxf包下的bin目录(是为了使用该目录下的指令)。2、在命令行敲上:wsdl2java http://localhost:8080/calculator?wsdl 。完了以后再看这个bin目录,你会发现多了一个com文件夹,这个就是产生的项目代码文件,留着等下用。3、新建一个项目作为客户端,同样把所有CXF的jar包都导入,接着把上一步产生的com文件夹剪切到项目的src目录下。 把有出现红线的地方按提示修改就行了。4、在同一个包下面编写Client类,在main方法调用服务。最后运行Run as Java Application。







package com.likunjie;

public class Client {
public static void main(String[] args) {
Calculator calc = new Calculator();
CalculatorService service = calc.getCalculatorServiceImplPort();
int a = service.add(3, 7);
String b = service.concat("what's ", "that about?");
System.out.println(a);
System.out.println(b);
}
}




输出结果:

10
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: