关闭

webservice 发布的接口不能使“多态”

301人阅读 评论(0) 收藏 举报
分类:

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);
}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:313996次
    • 积分:8755
    • 等级:
    • 排名:第2259名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:5条
    最新评论