创建WebService项目
要创建一个 Web 服务项目中,我们使用了Eclipse IDE。首先编写服务端接口 HelloWorld 包下com.yiibai.ws。 HelloWorld.java 的内容如下:
package com.yiibai.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface @WebService @SOAPBinding(style = Style.RPC) public interface HelloWorld{ @WebMethod String getHelloWorldMessage(String string); } |
这个服务有一个的方法 getHelloWorldMessage 需要字符串 参数。
下一步创建实现类 HelloWorldImpl.java 在包 com.yiibai.ws 下。
package com.yiibai.ws; import javax.jws.WebService; @WebService(endpointInterface="com.yiibai.ws.HelloWorld") public class HelloWorldImpl implements HelloWorld { @Override public String getHelloWorldMessage(String myName){ return("Hello "+myName+" to JAX WS world"); } } |
作为下一个步骤,让本地发布此Web服务通过创建端点出版商和公开此服务的服务器上。
publish方法有两个参数:
端点URL字符串。
实现程序对象 HelloWorld 实现类,在这种情况下,这是作为一个Web服务公开在以上参数中提到的由URL标识的端点。
HelloWorldPublisher.java 内容如下所示:
package com.yiibai.endpoint; import javax.xml.ws.Endpoint; import com.yiibai.ws.HelloWorldImpl; public class HelloWorldPublisher { public static void main(String[] args){ Endpoint.publish("http://localhost:9000/ws/hello", new HelloWorldImpl()); } } |