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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值