如何发布一个WebService

Jdk版本  java1.6_45

 1 import javax.jws.WebMethod;
 2 import javax.jws.WebService;
 3 import javax.xml.ws.Endpoint;
 4 
 5 @WebService//给类添加注解 
6 public class HelloWorldService { 7 8 public String sayHello(String name){//声明实例方法 9 return "hello: "+name; 10 11 } 12 @WebMethod(exclude=true) 13 public String sayHello2(String name){ 14 return "hello :"+name; 15 16 } 17 18 public static void main(String[] args) { 19 /** 20 * 使用端点服务,将对象绑定到一个地址和端口。同时必须要在端口后面给服务取一个名称 21 */ 22 Endpoint.publish("http://127.0.0.1:6789/hello", new HelloWorldService()); 23 System.out.println("ready on..."); 24 } 25 }

 

注意

给类添加上@WebService注解后,类中所有的非静态方法都将会对外公布。不支持静态方法,final方法。如果希望某个方法(非static,非final)不对外公开,可以在方法上添加@WebMethod(exclude=true),阻止对外公开。如果一个类上,被添加了@WebService注解,则必须此类至少有一个可以公开的方法,否则将会启动失败。


 


posted @ 2014-10-17 15:07  半杯清茶  阅读(767)  评论(0)    收藏  举报