webservice应用

前端之家收集整理的这篇文章主要介绍了webservice应用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_403_8@什么是webservice

webservice是一种跨编程语言跨操作系统平台远程调用技术

Web服务:基于HTTP和XML的技术,HTTP是互联网上应用最为广泛的一种网络协议,而XML是跨平台的基础。

Java 就有 Apache Axis1、Apache Axis2、Codehaus XFire、Apache CXF、Apache Wink、Jboss  RESTEasyd等等

WebService三要素

SOAP (Simple Object Access Protocol):简易对象访问协议,soap用来描述传递信息的格式。

WSDL (WebServices Description Language):Web服务描述语言,用来描述如何访问具体的接口。

UDDI (Universal Description Discovery and Integration):通用描述、发现及整合,用来管理、分发、查询webService。

WebService的应用场合

跨越防火墙通信@H_403_8@:应用程序可以使用HTTP和XML消息等标准在基于Web的应用程序之间交换信息,从而跨越防火墙

应用程序集成@H_403_8@:何应用程序理论上都可以通过SOAP消息与任何其他应用程序进行通信

软件复用@H_403_8@:软件复用是在软件开发中避免重复劳动的解决方

WebService开发实战

一、原生态客户端编写

1.打开cmd,执行wsimport生成代码

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

2.如图所示,代码生成在D:\MyTest\webservice。会生成class和java文件删除class文件

 

分享图片

3、开始编写查询手机号归属地客户端 

eclipse创建工程,将生成代码copy到你自己想要的包下@H_403_8@,然后编写第一个WebService客户端接口

 

分享图片

public class PhoneAddressClient {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(getPhoneAddress("15501529XXX"));
	}

	public static String getPhoneAddress(String phone) {
		// 1.实例化生成的服务类
		MobileCodeWS ws = new MobileCodeWS();
		// 2.调用服务类的方法获取接口实例
		MobileCodeWSSoap soap = ws.getMobileCodeWSSoap();
		// 3.通过接口直接获取数据
		return soap.getMobileCodeInfo(phone,"");
	}
}
15501529XXX:江苏 苏州 江苏联通GSM卡

@H_403_8@二、自定义WebService服务端并发布服务编写客户端测试 

1.编写服务接口

/**
 * 服务端
 * @author zhoudoujun01
 *
 */
public interface FisrtWebService {
	String getAddressByPhoneNo(String phoneNo);
}

2.编写接口实现类

@WebService
public class FisrtWebServiceImpl implements FisrtWebService {

	public String getAddressByPhoneNo(String phoneNo) {
		 return phoneNo + ": 归属地是上海";
	}

}

3.发布服务

public class PublishServerTest {

	public static void main(String[] args) {
		//参数1:服务被访问的url   ip+端口号+服务名
        //参数2:实现类
        Endpoint.publish("http://localhost:9999/getAddress",new FisrtWebServiceImpl());
        System.out.println("服务发布成功");
	}

}

4.验证服务是否发布成功 
访问发布服务的ip+端口+服务名+?wsdl 

http://localhost:9999/getAddress?wsdl

分享图片

5.@H_403_8@编写客户端(@H_403_8@同上操作@H_403_8@)

 

三、利用cxf开发webservice

Apache CXF = Celtix + XFire@H_403_8@,Apache CXF @H_403_8@的前身叫 Apache CeltiXfire@H_403_8@,现在已经正式更名为 Apache CXF @H_403_8@了,以下简称为 CXF

@H_403_8@支持多种标准

@H_403_8@多种传输方式、Bindings@H_403_8@、Data Bindings @H_403_8@和 Format

  • Bindings@H_403_8@:SOAP@H_403_8@、REST/HTTP@H_403_8@;
  • Data Bndings@H_403_8@:目前支持 JAXB 2.0@H_403_8@、Aegis @H_403_8@两种,默认是 JAXB 2.0@H_403_8@。XMLBeans@H_403_8@、Castor @H_403_8@和 JiBX @H_403_8@数据绑定方式将在 CXF 2.1 @H_403_8@版本中得到支持
  • @H_403_8@格式(Format@H_403_8@):XML@H_403_8@、JSON@H_403_8@;
  • @H_403_8@传输方式:HTTP@H_403_8@、Servlet@H_403_8@、JMS @H_403_8@和 Jabber@H_403_8@;
  • @H_403_8@可扩展的 API @H_403_8@允许为 CXF @H_403_8@增加其它的 Bindings@H_403_8@,以能够支持其它的消息格式,比如:CSV @H_403_8@和固定记录长度。 

@H_403_8@开发步骤:

1、@H_403_8@新建工程maven web@H_403_8@:cxf_spring@H_403_8@,导入pom.xml

<properties>
	  <cxf.version>3.0.8</cxf.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<type>jar</type>
			<scope>test</scope>
		</dependency>
		
		<!-- spring的jar包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		
		<!-- cxf的jar包 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<dependency>
	        <groupId>org.apache.cxf</groupId>
	        <artifactId>cxf-rt-transports-http</artifactId>
	        <version>${cxf.version}</version>
	    </dependency>
	    <dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.geronimo.specs</groupId>
			<artifactId>geronimo-ws-Metadata_2.0_spec</artifactId>
			<version>1.1.3</version>
		</dependency>
	</dependencies>
  <build>
    <finalName>cxf_spring</finalName>
  </build>

2、web.xml@H_403_8@添加

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath*:spring-cxf-config.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>  
        <load-on-startup>1</load-on-startup>
    </servlet>  
    <servlet-mapping>  
        <servlet-name>cxf</servlet-name>  
        <url-pattern>/*</url-pattern>  
    </servlet-mapping>
</web-app>

 

3、@H_403_8@新建applicationContext-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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <!--  Cxf WebService 服务端示例 -->
    <jaxws:endpoint id="Helloservice" implementor="com.adou.service.impl.HelloServiceImpl" address="/hello"/>
</beans>

 

@H_403_8@4、新建IHelloService

@WebService
public interface IHelloService {

    @WebMethod
    String sayHello(@WebParam(name="username")String username);
}
public class HelloServiceImpl implements IHelloService {

    public String sayHello(String username) {
        return "hello " + username;
    }
}

 

5、@H_403_8@启动测试

http://localhost:8080/cxf_spring

@H_403_8@

分享图片

6@H_403_8@、客户端调用TestHellServiceImpl

 

public class TestHellServiceImpl {
	@Test
	public void sayHello(){
		IHelloService helloService = new HelloServiceImpl();
		System.out.println(helloService.sayHello("tom"));
	}
}

7、Demo地址:cxf-spring  

猜你在找的WebService相关文章