1、定义一个多态的接口
package hb; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHelloWorldService { public String sayHello(@WebParam(name = "name") String name); public String sayHello(@WebParam(name = "name") String name,@WebParam(name = "country") String country); }
?
2、定义一个对接口的实现类
package hb; import javax.jws.WebParam; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style = Style.RPC) public class HelloWorldService implements IHelloWorldService { public String sayHello(@WebParam(name = "name") String name) { return name + " say: Hello World "; } @Override public String sayHello(@WebParam(name = "name") String name, @WebParam(name = "country") String country) { System.out.println("public String sayHello(@WebParam(name = \"name\") String name," + "@WebParam(name = \"country\") String country)"); return null; } }
?
3、发布webservice
package hb; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class HelloWorldServiceClient { public static void main(String[] args) { //调用WebService JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(IHelloWorldService.class); factory.setAddress("http://localhost:9000/helloWorld"); IHelloWorldService service = (IHelloWorldService) factory.create(); System.out.println("[result]" + service.sayHello("huangbiao")); } }
?
启动服务会报错:An operation with name [{http://hb/}sayHello] already exists in this service;即该接口已经发布了,解决办法就是保证对外提供的接口名称必须是唯一的。
?
解决办法:修改接口,保证名称唯一。
package hb; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHelloWorldService { public String sayHello(@WebParam(name = "name") String name); public String sayHello1(@WebParam(name = "name") String name,@WebParam(name = "country") String country); }
?
?
?