java cxf 工具_利用CXF工具开发WebService接口
簡單記錄一下webservice接口開發以便供以后參考:
一、根據需求編寫wsdl文件
WSDL的文件格式和語法我就不多說了,到網上百度一下,或者到W3CSchool去學習都可以,語法很簡單,下面是我自己寫的一個demo,僅供參考:
WSDL文件:
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">
elementFormDefault="qualified" targetNamespace="http://hr.soa.csg.cn"
xmlns:tns="http://hr.soa.csg.cn" xmlns:xs="http://www.w3.org/2001/XMLSchema">
nillable="true" type="tns:User" />
transport="http://schemas.xmlsoap.org/soap/http" />
二、利用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 生成所有開始端點代碼
利用CXF工具生成代碼命令
生成的代碼列表
把所有類中注解部分有紅色標注的這一行代碼刪掉
三、接口代碼測試
(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,截圖如下:
看到的基本上就是和我們寫的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)
總結
以上是生活随笔為你收集整理的java cxf 工具_利用CXF工具开发WebService接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tf.train.Exponential
- 下一篇: tf.nn.in_top_k的用法