cxf远程调用服务

wsimport命令用法

wsimport是由jdk提供的命令,作用是解析wsdl文件,生成本地代码

wsimport –s . (将生成的代码写当前目录) http://10.254.1.53:8080/hello?wsdl(必须 wsdl地址)

-d 生成class文件

-p 生成包名

-s 生成java文件+class文件

把网络上面的webservice服务代码下载下来自己调用

wsimport -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

拷贝项目,添加测试方法调用

public static void main(String[] args) {
	
	MobileCodeWS mc =new MobileCodeWS();
	MobileCodeWSSoap wsSoap = mc.getMobileCodeWSSoap();
	String info = wsSoap.getMobileCodeInfo("1338039", "");
	System.out.println(info);
}

自已发布一个webservice项目

自己创建一个webservice项目

@WebService//添加注解就是个webservice项目了
public class HelloService {
public String sayHello(String name)
{
	System.out.println("sayhello方法执行了");
	return "hello"+name;
}
public static void main(String[] args) {
	Endpoint.publish("http://192.168.25.56:8080/MyWebService", new HelloService());//指定服务器发布地址,和服务端提供服务的实例对象
	System.out.println("服务器发布好了");
}
}

用浏览器打开填写的地址

复制wsdl地址

下载就能得到java文件

wsimport -s . -p com.itheima.service http://192.168.63.1:8080/MyWebService?wsdl

运行自己发布的webservice

HelloServiceService service = new HelloServiceService();
	HelloService helloService = service.getHelloServicePort();//得到原来的实体类
	String string = helloService.sayHello("rose");//执行原来的方法
	System.out.println(string);

CXF

 wsdl2Java命令使用:

CXF框架提供一个和jdk提供的wsimport命令功能类似的命令wsdl2java,可以解析wsdl文件,生成本地代码

 此工具位于cxf_home/bin目录下。参数与wsimport有所不同。

 它包含以下参数:

• -d参数,指定代码生成的目录。

• -p参数,指定生成的新的包结构。

 在命令行执行:

​ wsdl2java –d . http://127.0.0.1:6666/helloworld?wsdl

注意:没有-s参数

wsdl2java -d . -p com.itheima.cxf http://localhost/HelloCXF/cxf/hello?wsdl

创建一个cxf的webService服务端

导入cxf必须的jar包

web.xml里面配置cxf

<!--配置cxf  -->
    <servlet>
  	<servlet-name>cxf</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  	<!-- 由初始化参数指定配置文件 -->
  	<init-param>
  		<param-name>config-location</param-name>
  		<param-value>classpath:applicationContext.xml</param-value>//放在classpath下面
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>cxf</servlet-name>
  	<url-pattern>/cxf/*</url-pattern>
  </servlet-mapping>
  

两种cxf的applicationContext.xml配置方式

<param-value>classpath:applicationContext.xml</param-value>//放在classpath下面
<param-value>/WEB-INF/applicationContext.xml</param-value>//放在WEB-INF下面

创建一添加了@WebService的接口,和一个实现类,里面有个方法

通过xml注册服务

第一种:
<jaxws:endpoint address="/hello" implementorClass="com.itheima.serviceImpl.HelloServiceImpl" >
	</jaxws:endpoint> 

第二种
 <bean id="helloService" class="com.itheima.serviceImpl.HelloServiceImpl"/> 
	<!-- 注册服务 -->
	 	<jaxws:server id="myService" address="/hello">
		<jaxws:serviceBean>
			<ref bean="helloService"/>
		</jaxws:serviceBean>
	</jaxws:server>

指定参数的名字

在接口里面声明@WebParam(name="name")

public String sayHello( @WebParam(name="name") String name);

客户端使用

使用wsdl2java或者wsimport命令生成本地代码

只需要接口.class

客户端xml配置

<jaxws:client id="myProxy" address="http://localhost/HelloCXF/cxf/hello?wsdl" serviceClass="com.itheima.cxf.IHelloService">
</jaxws:client>

执行

	ClassPathXmlApplicationContext classPathXmlApplicationContext = new//加载配置文件 ClassPathXmlApplicationContext("applicationContext.xml");

	IHelloService bean = (IHelloService)classPathXmlApplicationContext.getBean("myProxy");
	String sayHello = bean.sayHello("老王");
	System.out.println(sayHello);

使用maven命令将本地的jar包安装到maven仓库中

mvn install:install-file -Dfile=crm3.jar -DgroupId=com.itcast -DartifactId=crm -Dversion=4.0 -Dpackaging=jar
posted @ 2017-08-17 11:14  猥琐熊花子酱  阅读(515)  评论(0)    收藏  举报