cxf服务端+axis2 1.6客户端配置

1. java web service(cxf)服务端配置说明

1.项目环境

struts2+hibernate3+spring2.5

cxf版本为apache-cxf-2.3.3.zip

2.配置步骤

a.在项目web.xml文件中加入CXF的servlet



说明:@WebService注解,标注此接口为web service;

@WebParam注解,标注方中参数名称

@SOAPBinding(style = Style.RPC)注解,标注数据的传输方式



@WebService

@SOAPBinding(style = Style.RPC)

publicclass ComplexUserServiceImplimplements IComplexUserService{

public StringgetUserByName(@WebParam(name = "name")

String name) {

try {

String n8 = newString(name.getBytes(), "utf-8");

System.out.println("getUserByName(1)" + n8);

System.out.println("getUserByName(2)"

+ new String(name.getBytes(), "utf-16"));

System.out.println("getUserByName(3)"

+ new String(name.getBytes(), "gbk"));

System.out.println("getUserByName(4)"

+ new String(name.getBytes(), "gb2312"));



System.out.println(name);

String s = new String("我是中文 THISCODE".getBytes("gbk"));

return s;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

return"";

}

}

c. webservice发布配置

增加(同spring配置文件一样)applicationContext-server.xml

<?xml version="1.0"encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:jaxws="http://cxf.apache.org/jaxws"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:cxf="http://cxf.apache.org/core"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.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"/>





<bean id="userServiceBean" class="com.webservice.impl.ComplexUserServiceImpl"></bean>

<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>



<jaxws:server id="userServiceWS" serviceClass="com.webservice.IComplexUserService"address="/Users">

<jaxws:serviceBean>

<ref bean="userServiceBean"/>

</jaxws:serviceBean>

<jaxws:outInterceptors>

<ref bean="outLoggingInterceptor"/>

</jaxws:outInterceptors>

</jaxws:server>

</beans>

d. 结束语

至此,启动tomcat,cxf web service会随tomcat服务器启动而自动发布cxf服务

访问地址为http://localhost:端口/项目名称/ cxf-webservice/Users?wsdl,

如果能访问到wsdl文件,说明cxf服务部署成功






2. web service客户端配置

1. 项目环境

同上

axis所需jar包:

axis.jar axis2-kernel-1.6.2.jar commons-discovery-0.2.jar commons-logging-1.1.1.jar wsdl4j-1.6.2.jar

2. 部署步骤

注:本打算用CXF做客户端访问c++(gsoap)的web service服务端,无奈gsoap发布出 wsdl文档编码格式为encoded,而CXF已不支持encoded的编码格式。如果要连 gsoap 的web service服务店,必须换用其它框架。

这里直接写Main函数,连接gsoap服务器即可

publicstaticvoid main(String[] args) throws UnsupportedEncodingException {

try {

String endpoint = "http://192.168.1.114:8000/";

Service service = new Service();

Call call = (Call) service.createCall();

call.setTargetEndpointAddress(endpoint);

// QName第一个参数为wdsl文档的命名空间,

// 第二个参数为wsdl发布的方法名称

call.setOperationName(new QName("urn:calc", "getbyname"));

// addParameter第一个参数为wsdl发布的参数名称

call.addParameter("input", null, javax.xml.rpc.ParameterMode.IN);

// 标明返回的类型是什么

call.setReturnClass(String.class);

// 设置传递的参数值

String temp = (String) call.invoke(new Object[] { new String("<?xml version=\"1.0\"encoding=\"UTF-8\"?><xml n='me'>is中文</xml>".getBytes("gbk")) });



String n8 = new String(temp.getBytes(), "utf-8");

System.out.println("getUserByName(1)" + n8);

System.out.println("getUserByName(2)"

+ new String(temp.getBytes(), "utf-16"));

System.out.println("getUserByName(3)"

+ new String(temp.getBytes(), "gbk"));

System.out.println("getUserByName(4)"

+ new String(temp.getBytes(), "gb2312"));

}



} catch (ServiceException e) {

// TODO Auto-generatedcatch block

e.printStackTrace();

} catch (RemoteException e) {

// TODO Auto-generatedcatch block

e.printStackTrace();

}



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值