java cxf 設置服務端及客戶端

一.CXF通過文件生成客戶端

1.在瀏覽器中打開webservice url,保存wsdl文件,如ContentService.xml


2.通過cxf命令“wsdl2java –d E:/file –frontend jaxws21 –client C:\ ContentService.xml”生成客戶端代碼


3.若報WSDLToJava Error: Thrown by JAXB : undefined simple or complex type 'soap-enc:Array' ,
則需要在生成的文件中找到
<import namespace="http://schemas.xmlsoap.org/soap/encoding/" />,
在瀏覽器中打開http://schemas.xmlsoap.org/soap/encoding/,
保存文件soap-encoding.xsd,
然後修改成<import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="soap-encoding.xsd"/>,

再運行上面的命令即可。

4.如果需要NTLM認證,則需在代碼中加入如下的代碼:

Client client = ClientProxy.getClient(port);


HTTPConduit http = (HTTPConduit) client.getConduit();


HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); 


httpClientPolicy.setConnectionTimeout(36000);


httpClientPolicy.setAllowChunking(false);


http.setClient(httpClientPolicy);


http.getAuthorization().setAuthorizationType("NTLM"); 


http.getAuthorization().setUserName("xxxx");


http.getAuthorization().setPassword("xxxxx");


二:CXF生成服務端
1.服務接口:
package cn.com.td.smsIssued.service;


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


@WebService
public interface ISmsIssued {
public String smsIssued(@WebParam(name="mobile")String mobile, @WebParam(name="message")String message);
}
2.接口實現類
package cn.com.td.smsIssued.service.impl;




import javax.jws.WebService;




import cn.com.td.smsIssued.service.ISmsIssued;
import cn.com.td.smsIssued.socketClient.SocketClient;




@WebService(endpointInterface = "cn.com.td.smsIssued.service.ISmsIssued")
public class SmsIssuedService implements ISmsIssued{




public String smsIssued(String mobile, String message) {
try {
String s = SocketClient.requestSocketService(mobile, message);
return s;
} catch (Exception e) {
e.printStackTrace();
}
return "0";
}


}


3.如果存在JAVA對象,需要進行序列化處理:
@XmlAccessorType(XmlAccessType.FIELD)  
public class Person implements Serializable


4.通過spring整合的需要添加配置文件applicationContext_cxf.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:soap="http://cxf.apache.org/bindings/soap"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd">
 
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <!-- 這三行的配置不用去檢查對應的路徑下是否有對應的文件,因爲cxf會自動生成的-->
 
<bean id="smsIssuedService" class="cn.com.td.smsIssued.service.impl.SmsIssuedService"/>
<!-- smsIssuedService就不用解釋了,這是典型的Spring配置,開發時建議採用Spring2.5的標註 -->
    <jaxws:endpoint id="smsIssuedWebService" implementor="#smsIssuedService" address="/smsIssuedService">
    </jaxws:endpoint>
</beans>


4.web(web.xml)服務的配置:
<context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath*:applicationContext_cxf.xml</param-value>
    </context-param> 
    <listener>
       <listener-class>
           org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>
    <!--前面兩個就不用多說了,下面還是我們要關注的CXF配置-->
    <servlet>
       <servlet-name>CXFServlet</servlet-name>
       <servlet-class>
           org.apache.cxf.transport.servlet.CXFServlet
       </servlet-class>
       <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
       <servlet-name>CXFServlet</servlet-name>
       <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

發佈了57 篇原創文章 · 獲贊 26 · 訪問量 51萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章