JAX-WS是一套Java用于开发XML Web Services的技术规范,它的实现一般有CXF、AXIS和JDK(version>=1.6),借助这些我们可以进行SOAP服务开发。
CXF和AXIS是常见的WebService开发框架,这里我们使用JDK自带的注解简单实现一个服务并发布。
服务接口类:IHello.java
package com.lichmama.ws.intf; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHello { @WebMethod(operationName="sayHi") public String sayHello(@WebParam(name="name") String name); }
服务实现类:HelloImpl.java
package com.lichmama.ws.impl; import javax.jws.WebService; import com.lichmama.ws.intf.IHello; @WebService(endpointInterface="com.lichmama.ws.intf.IHello", serviceName="HelloService") public class HelloImpl implements IHello { @Override public String sayHello(String name) { System.out.println("name is " + name); if (name == null || name.isEmpty()) { name = "anonymous"; } return "hello, " + name; } }
发布服务:
public class TestCase { public static void main(String[] args) { String serviceURL = "http://localhost:8080/ws/hello"; Endpoint.publish(serviceURL, new HelloImpl()); } }
访问http://localhost:8080/ws/hello?wsdl
参考文档: