简单记录一下webservice接口开发以便供以后参考:
一、根据需求编写wsdl文件
WSDL的文件格式和语法我就不多说了,到网上百度一下,或者到W3CSchool去学习都可以,语法很简单,下面是我自己写的一个demo,仅供参考:
WSDL文件:
<?xml version='1.0' encoding='UTF-8'?>
<wsdl:definitions name="QueryUserInfoService"
targetNamespace="http://hr.soa.csg.cn" xmlns:ns1="http://cxf.apache.org/bindings/xformat"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://hr.soa.csg.cn"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" targetNamespace="http://hr.soa.csg.cn"
xmlns:tns="http://hr.soa.csg.cn" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="queryUserInfoRequest" type="tns:QueryUserInfoRequest" />
<xs:complexType name="QueryUserInfoRequest">
<xs:sequence>
<xsd:element minOccurs="0" name="userId" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="queryUserInfoResponse" type="tns:QueryUserInfoResponse" />
<xs:complexType name="QueryUserInfoResponse">
<xs:sequence>
<xs:element minOccurs="0" name="replyCode" type="xs:string" />
<xs:element maxOccurs="unbounded" minOccurs="0" name="user"
nillable="true" type="tns:User" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="User">
<xs:sequence>
<xs:element name="userId" type="xs:string" />
<xs:element name="age" type="xs:string" />
<xs:element name="name" type="xs:string" />
<xs:element name="email" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<!--查询用户信息-->
<wsdl:message name="queryUserInfoRequest">
<wsdl:part name="queryUserInfoRequest" element="tns:queryUserInfoRequest" />
</wsdl:message>
<wsdl:message name="queryUserInfoResponse">
<wsdl:part name="queryUserInfoResponse" element="tns:queryUserInfoResponse" />
</wsdl:message>
<wsdl:portType name="SOAServicePort">
<wsdl:operation name="queryUserInfo">
<wsdl:input message="tns:queryUserInfoRequest" />
<wsdl:output message="tns:queryUserInfoResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="SOAServiceBinding" type="tns:SOAServicePort">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="queryUserInfo">
<soap:operation style="document" soapAction="" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="QueryUserInfoService">
<wsdl:port name="SOAService" binding="tns:SOAServiceBinding">
<soap:address location="http://localhost:8080/service/queryUserInfoService" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
二、利用CXF工具生成接口代码
1、下载cxf工具
下载地址:http://cxf.apache.org/download.html
(1)解压cxf工具,使用控制台(cmd)进入bin目录
2、使用wsdl2java工具生成客户端代码
(2)wsdl2java用法: wsdl2java -p com.client -d d:\client\src –all transferQueryEmpCode.wsdl
(注:-p后面的参数是生成代码所在的包名,-d后面的参数是生成代码存放的路径,-all后面的参数是wsdl文件所在的具体目录)
-p 指定其wsdl的命名空间,也就是要生成代码的包名:
-d 指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-all 生成所有开始端点代码
三、接口代码测试
(1)运行服务端测试类,发布接口:
public class SOAServicePort_SOAService_Server{
protected SOAServicePort_SOAService_Server() throws Exception {
System.out.println("Starting Server");
Object implementor = new SOAServicePortImpl();
//接口发布地址
String address = "http://127.0.0.1:8080/service/queryUserInfoService";
Endpoint.publish(address, implementor);
}
public static void main(String args[]) throws Exception {
new SOAServicePort_SOAService_Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
(2)在浏览器输入发布地址拼上?wsdl,截图如下:
(3)简单实现一下服务端的实现类
public class SOAServicePortImpl implements SOAServicePort {
private static final Logger LOG = Logger.getLogger(SOAServicePortImpl.class.getName());
/* (non-Javadoc)
* @see com.user.server.SOAServicePort#queryUserInfo(com.user.server.QueryUserInfoRequest queryUserInfoRequest )*
*/
public com.user.server.QueryUserInfoResponse queryUserInfo(QueryUserInfoRequest queryUserInfoRequest) {
LOG.info("Executing operation queryUserInfo");
System.out.println(queryUserInfoRequest.getUserId());
try {
//简单实现一下服务端,返回给调用者的数据
com.user.server.QueryUserInfoResponse _return = new QueryUserInfoResponse();
_return.setReplyCode("200");
User u = new User();
u.setAge("11");
u.setEmail("xxx@126.com");
u.setName("TestUser");
u.setUserId("10086");
_return.getUser().add(u);
return _return;
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
}
(4)客户端测试类,简单封装以下请求参数
public final class SOAServicePort_SOAService_Client {
private static final QName SERVICE_NAME = new QName("http://hr.soa.csg.cn", "QueryUserInfoService");
private SOAServicePort_SOAService_Client() {
}
public static void main(String args[]) throws Exception {
//需要将WSDL_LOCATION修改为我们发布的地址:http://127.0.0.1:8080/service/queryUserInfoService?wsdl
URL wsdlURL = QueryUserInfoService.WSDL_LOCATION;
if (args.length > 0) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
QueryUserInfoService ss = new QueryUserInfoService(wsdlURL, SERVICE_NAME);
SOAServicePort port = ss.getSOAService();
{
System.out.println("Invoking queryUserInfo...");
//简单封装请求参数
com.user.server.QueryUserInfoRequest _queryUserInfo_queryUserInfoRequest = new QueryUserInfoRequest();
_queryUserInfo_queryUserInfoRequest.setUserId("10086");
//下面这一句代码就是调用服务端接口的代码
com.user.server.QueryUserInfoResponse _queryUserInfo__return = port.queryUserInfo(_queryUserInfo_queryUserInfoRequest);
System.out.println("queryUserInfo.result=" + _queryUserInfo__return.getReplyCode());
System.out.println(_queryUserInfo__return.getUser().get(0).toString());
}
System.exit(0);
}
}
(5)测试结果截图
(6)以上只是简单的测试,还未涉及到cxf集成spring发布接口
四、其他注意事项
1、生成的接口代码出现响应报文缺少命名空间前缀问题:
解决办法:
(1)在生成的ObjectFactory.java
类中修改一下代码:
@XmlElementDecl(namespace = "http://test.cxf.com", name = "queryEmployeeCodeResponse")
中的namespace置为空(即是@XmlElementDecl(namespace = "", name = "queryEmployeeCodeResponse")
)
(2)在生成的package-info.java
类的注解中增加一下代码:
原来的注解(@javax.xml.bind.annotation.XmlSchema(namespace = "http://hr.soa.csg.cn")
)
增加后的注解:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://hr.soa.csg.cn", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)