当前位置:编程学习 > 网站相关 >>

webservice快速入门(一)

WEBSERVICE快速入门的示例:
 
首先定义接口:
 
 
[java] 
package com.whaty.platform.ws.server;  
  
import javax.jws.WebService;  
  
/** 
 * @className:IMyservice.java 
 * @Desc:定义:SEI service endpoint inte易做图ce 
 * @author:lizhuang 
 * @createTime:2012-12-21 上午12:57:18 
 */  
//JAX-WS注解,表示java api xml for webservice。JDK自带API的XML格式的webservice   
@WebService  
public inte易做图ce IMyservice {  
      
    int add(int a, int b);  
  
    int minus(int a, int b);  
}  
 
package com.whaty.platform.ws.server;
 
import javax.jws.WebService;
 
/**
 * @className:IMyservice.java
 * @Desc:定义:SEI service endpoint inte易做图ce
 * @author:lizhuang
 * @createTime:2012-12-21 上午12:57:18
 */
//JAX-WS注解,表示java api xml for webservice。JDK自带API的XML格式的webservice
@WebService
public inte易做图ce IMyservice {
 
int add(int a, int b);
 
int minus(int a, int b);
}
 
其次编写实现类: 
 
 
[java] 
package com.whaty.platform.ws.server;  
  
import javax.jws.WebService;  
  
/**  
 * @className:MyServiceImpl.java 
 * @Desc:定义:SIB service implemention bean 
 * @author:lizhuang 
 * @createTime:2012-12-21 上午01:01:22 
 */  
 
package com.whaty.platform.ws.server;
 
import javax.jws.WebService;
 
/** 
 * @className:MyServiceImpl.java
 * @Desc:定义:SIB service implemention bean
 * @author:lizhuang
 * @createTime:2012-12-21 上午01:01:22
 */[java] view plaincopyprint?//endpointInte易做图ce指定接入点接口:接口必须存在   
@WebService(endpointInte易做图ce="com.whaty.platform.ws.server.IMyservice")  
public class MyServiceImpl implements IMyservice {  
  
    public int add(int a, int b) {  
        System.out.println("a+b="+(a+b));  
        return a+b;  
    }  
  
    public int minus(int a, int b) {  
        System.out.println("a-b="+(a-b));  
        return a-b;  
    }  
  
}  
 
//endpointInte易做图ce指定接入点接口:接口必须存在
@WebService(endpointInte易做图ce="com.whaty.platform.ws.server.IMyservice")
public class MyServiceImpl implements IMyservice {
 
public int add(int a, int b) {
System.out.println("a+b="+(a+b));
return a+b;
}
 
public int minus(int a, int b) {
System.out.println("a-b="+(a-b));
return a-b;
}
 
}
 
最后发布我们的服务,直接右键运行main方法,如果控制台没报错,多半是发布成功了,否则检查你的代码: 
 
 
[java] 
package com.whaty.platform.ws.server;  
  
import javax.xml.ws.Endpoint;  
  
/** 
 * @className:MyServer.java 
 * @Desc:发布服务 
 * @author:lizhuang 
 * @createTime:2012-12-21 上午01:02:39 
 */  
public class MyServer {  
    public static void main(String[] args) {  
        //访问方式:http://localhost:7777/tudou?wsdl   
        String address="http://localhost:7777/tudou";  
        Endpoint.publish(address, new MyServiceImpl());  
    }  
}  
 
package com.whaty.platform.ws.server;
 
import javax.xml.ws.Endpoint;
 
/**
 * @className:MyServer.java
 * @Desc:发布服务
 * @author:lizhuang
 * @createTime:2012-12-21 上午01:02:39
 */
public class MyServer {
public static void main(String[] args) {
//访问方式:http://localhost:7777/tudou?wsdl
String address="http://localhost:7777/tudou";
Endpoint.publish(address, new MyServiceImpl());
}
}
 
 
 
 
 
 
 
 
浏览器显示如下:
 
 
[html]
This XML file does not appear to have any style information associated with it. The document tree is shown below.  
<!-- 
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.  
-->  
<!-- 
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.  
-->  
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.ws.platform.whaty.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.ws.platform.whaty.com/" name="MyServiceImplService">  
<types>  
<xsd:schema>  
<xsd:import namespace="http://server.ws.platform.whaty.com/" schemaLocation="http://localhost:7777/tudou?xsd=1"/>  
</xsd:schema>  
</types>  
<message name="minus">  
<part name="parameters" element="tns:minus"/>  
</message>  
<message name="minusResponse">  
<part name="parameters" element="tns:minusResponse"/>  
</message>  
<message name="add">  
<part name="parameters" element="tns:add"/>  
</message>  
<message name="addResponse">  
<part name="parameters" element="tns:addResponse"/>  
</message>  
<portType name="IMyservice">  
<operation name="minus">  
<input message="tns:minus"/>  
<output message="tns:minusResponse"/>  
</operatio
补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,