咱们在上一章节中已经使用wsimport生成本地代理来调用webservice的服务了,其实咱们本身写的web应用程序也是能够发布webservice的java
咱们发布了webservice的话,那么其余人也是能够调用咱们本身写的webservice!android
那么咱们怎么自定义webservice而后发布出去呢???ios
在jdk 1.6 版本之后 ,经过jax-ws 包提供对webservice的支持 web
写一个实体:windows
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 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可以在浏览器访问markdown
目前WebService的协议主要有SOAP1.1和1.2。架构
Soa(Service-Oriented Architecture) :面向服务的架构,它是一种思想,IBM大力倡导是即插即用的,IBM大力提倡,但愿以组装电脑的方式来开发应用并发
组成:app
uddi (Universal Description, Discovery and Integration)统一描述、发现、集成