webservice第二篇【自定义webservice服务、soa、uddi概念、soap协议】
2018-01-21 00:00
225 查看
自定义webservice服务
我们在上一章节中已经使用wsimport生成本地代理来调用webservice的服务了,其实我们自己写的web应用程序也是可以发布webservice的我们发布了webservice的话,那么其他人也是可以调用我们自己写的webservice!
那么我们怎么自定义webservice然后发布出去呢???
在jdk 1.6 版本以后 ,通过jax-ws 包提供对webservice的支持
该方式通过注解的方式来声明webservice
通过 jdk EndPoint.publish()发布webserive服务
快速入门
写一个实体:public class Phone { private String name;//操作系统名 private String owner;//拥有者 private int total;//市场占有率 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } }
发布service,通过注解来让WSDL文件更加可读…
package cn.it.ws.d; import cn.it.ws.model.Phone; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.ws.Endpoint; /* *手机的业务类,该业务类通过webservice 对外提供服务 * 1. 声明: @webservice * 2. 发布 EndPoint */ @WebService (serviceName="PhoneManager",//修改服务名 targetNamespace="http://dd.ws.it.cn") //修改命名空间 ,默认包名,取反 //声明该业务类 对外提供webservice服务 ,默认只是对public 修饰的方法对外以webservice形式发布 public class PhoneService { /**@WebMethod(operationName="getMObileInfo"): 修改方法名 * @WebResult(name="phone"):修改返回参数名 * @WebParam(name="osName"):修改输入参数名 */ @WebMethod(operationName="getMObileInfo") public @WebResult(name="phone") Phone getPhoneInfo(@WebParam(name="osName")String osName){ Phone phone=new Phone(); if(osName.endsWith("android")){ phone.setName("android");phone.setOwner("google");phone.setTotal(80); }else if(osName.endsWith("ios")){ phone.setName("ios");phone.setOwner("apple");phone.setTotal(15); }else{ phone.setName("windows phone");phone.setOwner("microsoft");phone.setTotal(5); } return phone; } @WebMethod(exclude=true)//把该方法排除在外 public void sayHello(String city){ System.out.println("你好:"+city); } private void sayLuck(String city){ System.out.println("好友:"+city); } void sayGoodBye(String city){ System.out.println("拜拜:"+city); } protected void saySayalala(String city){ System.out.println("再见!"+city); } public static void main(String[] args) { String address1="http://127.0.0.1:8888/ws/phoneService"; // String address2="http://127.0.0.1:8888/ws/phoneManager"; /** * 发布webservice服务 * 1.address:服务的地址 * 2:implementor 服务的实现对象 */ Endpoint.publish(address1, new PhoneService()); // Endpoint.publish(address2, new PhoneService()); System.out.println("wsdl地址 :"+address1+"?WSDL"); } }
在类上添加@WebService注解,代表发布一个WebService服务
通过EndPoint(端点服务)发布一个webService。Endpoint也是jdk提供的一个专门用于发布服务的类,它的publish方法接收两个参数,一个是本地的服务地址,二是提供服务的类。它位于javax.xml.ws.*包中。
Endpoint.publish(String address, Object implementor) 静态方法在给定地址处针对指定的实现者对象创建并发布端点
给类添加上@WebService注解后,类中所有的非静态方法都将会对外公布
如果希望某个方法不对外公开,可以在方法上添加@WebMethod(exclude=true),阻止对外公开。
如果一个类上,被添加了@WebService注解,则必须此类至少有一个可以公开的方法,否则将会启动失败。
protected、private、final、static方法不能对外公开
@WebService // 添加了此注解,代表是一个WebService public class HelloWorld { // 非 static final private 方法默认会发布 public String sayHi(String name) { return "hello" + name; } @WebMethod(exclude=true) public void exclude(){ // 被注解排除的方法 } protected void protected1(){ //受保护的方法默认不发布 } private void private1(){ // 私有方法默认不发布 } public static void static1(){ // static 方法默认不发布 } public final void final1(){ // final 方法默认不发布 } }
生成的webservice能够在浏览器访问
SOAP协议
目前WebService的协议主要有SOAP1.1和1.2。
两者的命名空间不同。
Soap1.1的命名空间:
xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/ “
Soap1.2 命名空间:
xmlns:soap=”http://www.w3.org/2003/05/soap-envelope“
SOAP1.1版本与SOAP1.2版本在头信息上存在差异。
SOAP1.1存在SOAPAction的请求头。
SOAP1.2没有SOAPAction的请求头。
基于SOAP1.1生成的WSDL和基于SOAP1.2生成的WSDL也不一样。
主要看命名空间。
在CXF中两种协议请求的方式也不一样。
1.1为content-Type:text/xm;charset=UTF-8
1.2为content-Type:application/soap+xml;charset=UTF-8
SOA、UDDI概念
SOA
Soa(Service-Oriented Architecture) :面向服务的架构,它是一种思想,IBM大力倡导是即插即用的,IBM大力提倡,希望以组装电脑的方式来开发应用组成:
面向web的服务,面向web的组件 :WebService : 硬盘、cpu、内存条
企业服务总线 (EnterPrise Service Bus :ESB)。主板
uddi
uddi (Universal Description, Discovery and Integration)统一描述、发现、集成它是目录服务,通过该服务可以注册和发布webservcie,以便第三方的调用者统一调用
相关文章推荐
- webservice第二篇【自定义webservice服务、soa、uddi概念、soap协议】
- 【转】SOA、WebService、UDDI、WSDL、SOAP、MSMQ概念
- SOA、WebService、UDDI、WSDL、SOAP、MSMQ概念
- 译著出版:《SOA概念、技术与设计》 - 面向服务的架构
- webservice--CXF+Spring整合发布SOAP协议的服务
- SOA面向服务架构——SOA的概念
- WebService、RMI、RPC、XML-RPC、JSON-RPC、SOAP、REST(rest-api、restful)等web服务实现方案概念
- SOA, SOAP, WebService概念区分
- 面向服务的体系结构(SOA) 之 目标与核心概念
- C++使用gsoap实现客户端访问webservice服务,概念知识普及
- 译著出版:《SOA概念、技术与设计》 - 面向服务的架构
- 使用WCF实现SOA面向服务编程——SOA的概念
- apache cxf-2.4.3 +spring-3.0.5发布SOAP协议WebService服务
- SOA面向服务架构——SOA的概念
- SOA面向服务架构——SOA的概念
- SOA面向服务架构——SOA的概念
- webService与分布式与微服务与SOA的关系
- 为什么要使用SOA面向服务的体系结构(为什么要使用webservice,dubbo)
- SOA & Webservice系列课程(1):面向服务的实质 -- 讲师: 吴延安
- UDDI and Beyond —— 服务注册和服务仓库在SOA中的角色