天天看点

WebService详解及CXF的使用

1.WebService简介

Webservice是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。

WebService详解及CXF的使用

WebService的特点:WebService通过HTTP POST方式接受客户的请求,WebService与客户端之间一般使用SOAP协议传输XML数据,它本身就是为了跨平台或跨语言而设计的

2.SOAP协议

SOAP(Simple Object Access Protocol)即简单对象访问协议,SOAP作为一个基于XML语言的协议用于在网上传输数据,SOAP = 在HTTP的基础上+XML数据,SOAP是基于HTTP的,SOAP的组成如下:1.Envelope – 必须的部分。以XML的根元素出现。2.Headers – 可选的。3.Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。

WebService详解及CXF的使用

请求实例代码

POST /WebServices/IpAddressSearchWebService.asmx HTTP/1.1
Host: ws.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getCountryCityByIp"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getCountryCityByIp xmlns="http://WebXml.com.cn/">//方法名
      <theIpAddress>string</theIpAddress>//参数
    </getCountryCityByIp>
  </soap:Body>
</soap:Envelope>
           

3.WSDL Web服务描述语言

WSDL(WebService Description Language):web 服务描述语言,就是一个xml文档,用于描述当前服务的一些信息(服务名称、服务的发布地址、服务提供的方法、方法的参数类型、方法的返回值类型等)。事例:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

WebService详解及CXF的使用

 4.基于jdk1.7发布一个WebService服务

1.创建java工程(webservice-service)作为服务端,并创建HelloService.java最为服务提供方

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/**
 *创建webService服务
 */
@WebService
public class HelloService {

	public String sayHello(String name) {
		System.out.println("服务端被调用了!!!!!!!!!!");
		return "hello "+name;
	}
	
	public static void main(String[] args) {
		//参一:服务地址    参二:提供服务的实例
		Endpoint.publish("http://192.168.25.1:8080/hello",new HelloService());
	}
	
}
           
WebService详解及CXF的使用

2.启动main方法,访问服务的文档WSDL:http://192.168.25.1:8080/hello?wsdl

WebService详解及CXF的使用

5.调用服务

1.jdk中wsimport的使用,作用就是解析wsdl文件,生成本地客户端代码

WebService详解及CXF的使用

wsimport是jdk自带的,可以根据wsdl文档生成客户端调用代码的工具.当然,无论服务器端的WebService是用什么语言写的,都将在客户端生成Java代码.服务器端用什么写的并不重要.wsimport.exe位于JAVA_HOME\bin目录下.

常用参数为:

-d<目录>  - 将生成.class文件。默认参数。

-s<目录> - 将生成.java文件。

-p<生成的新包名> -将生成的类,放于指定的包下。

(wsdlurl) - http://server:port/service?wsdl,必须的参数。

示例: C:/> wsimport –s . http://192.168.25.1/one?wsdl 注意:-s不能分开,-s后面有个小点,用于指定源代码生成的目录。点即当前目录。 如果使用了-s参数则会在目录下生成两份代码,一份为.class代码。一份为.java代码。 .class代码,可以经过打包以后使用。.java代码可以直接Copy到我们的项目中运行。

2.生成刚刚创建服务的客户端

WebService详解及CXF的使用
WebService详解及CXF的使用
WebService详解及CXF的使用

可以通过-p指定生成的包名

6.创建服务调用方,将刚刚生成的.java文件加入到工程中

WebService详解及CXF的使用

1.原理就是通过wsimport服务wsdl生成本地代码,通过本地代码创建服务的代理对象,通过代理对象调用远程服务

2.调用服务

public class App {
	public static void main(String[] args) {
		HelloServiceService helloServiceService = new HelloServiceService();
		HelloService helloServicePort = helloServiceService.getHelloServicePort();
		String sayHello = helloServicePort.sayHello("webservice");
		System.out.println(sayHello);
	}
}
           
WebService详解及CXF的使用
WebService详解及CXF的使用

注:调用时,服务都要启动,且生成的代码所在包要与服务方服务所在包相同

7.Apache CXF简介

Apache CXF = Celtix + Xfire

支持多种协议:

SOAP1.1,1.2

XML/HTTP

CORBA(Common Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的WS。C,c++,C#)

并可以与Spring进行快速无缝的整合 灵活的部署:可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,BeaWL上面

下载:http://cxf.apache.org/download.html    这里使用apache-cxf-2.4.2版

8.通过CXF发布服务

1.创建动态web工程,添加jar包,这里与spring整合,添加jar包较多

WebService详解及CXF的使用

2.在web.xml配置cxf提供的servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>cxf-service</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置监听器,spring启动时就加载cxf.xml -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:cxf.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <servlet>
  	<servlet-name>cxf</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  	<!-- 通过初始化参数指定CXF框架的配置文件位置 -->
  	<!-- <init-param>
  		<param-name>config-location</param-name>
  		<param-value>classpath:cxf.xml</param-value>
  	</init-param> -->
  </servlet>
  <servlet-mapping>
  	<servlet-name>cxf</servlet-name>
  	<!-- 拦截路径 -->
  	<url-pattern>/service/*</url-pattern>
  </servlet-mapping>
  
</web-app>
           

3.创建cxf.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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.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">
	<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
	<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" />

	
</beans>


           

4.开发一个接口及实现类

import javax.jws.WebService;

@WebService
public interface HelloService {
	String sayHello(String name);
}
           
import com.peng.demo.HelloService;

public class HelloServiceImpl implements HelloService {

	public String sayHello(String name) {
		System.out.println("基于cxf开发的服务端被调用了!!!!!!!!!");
		return "hello"+name;
	}

}
           

注:@WebService注解加载接口上

5.配置cxf.xml注册服务

<bean id="helloService" class="com.peng.demo.impl.HelloServiceImpl"/>
	
	<!-- 注册服务 -->
	<jaxws:server id="myService" address="/hello">
		<jaxws:serviceBean>
			<ref bean="helloService"/>
		</jaxws:serviceBean>
	</jaxws:server>
           
WebService详解及CXF的使用

6.启动服务,访问http://192.168.25.1:8080/cxf-service/service/hello?wsdl

WebService详解及CXF的使用

9.使用CXF调用服务

1.创建客户端项目,加入之前cxf服务端jar包

WebService详解及CXF的使用

2.通过wsimport或wsdl2java生成本地文件,这里使用wsdl2java命令,首先进入之前下载的CXF文件夹的bin目录下,之后生成文件

WebService详解及CXF的使用
WebService详解及CXF的使用
WebService详解及CXF的使用

3.将生成的接口拷贝到项目中

WebService详解及CXF的使用

发现报错,将ObjectFactory删除即可

WebService详解及CXF的使用

4.创建配置文件cxf.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.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">
	<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
	<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客户端的代理对象,通过spring创建代理对象,通过代理对象调用远程服务 -->
	<jaxws:client id="myClient" address="http://192.168.25.1:8080/cxf-service/service/hello" serviceClass="com.peng.demo.HelloService"></jaxws:client>
	
</beans>


           

5.测试调用

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
	public static void main(String[] args) {
		ApplicationContext ctx=new ClassPathXmlApplicationContext("cxf.xml");
		HelloService helloService = (com.peng.demo.HelloService) ctx.getBean("myClient");
		String sayHello = helloService.sayHello("cxf");
		System.out.println(sayHello);
	}
}
           
WebService详解及CXF的使用
WebService详解及CXF的使用

继续阅读