CXF异常:No operation was found with the name

情景描述:
CXFwebservice服务端:
webservice的接口类与实现类不再同一个包下:
接口类

package com.ainoties.core.webservice.sample;

import javax.jws.WebService;

import com.ainoties.core.webservice.sample.entity.User;

@WebService
public interface HelloCXF {

    public String sayHi(String username);

    public void hiUser(User user);
}

服务实现类(注意包名不同

package com.ainoties.core.webservice.sample.impl;

import javax.jws.WebService;

import com.ainoties.core.webservice.sample.HelloCXF;
import com.ainoties.core.webservice.sample.entity.User;

@WebService(
        endpointInterface="com.ainoties.core.webservice.sample.HelloCXF",
        serviceName="helloworld"
)
public class HelloCXFImpl implements HelloCXF {

    @Override
    public String sayHi(String username) {
        // TODO Auto-generated method stub
        return "hello "+username;
    }

    @Override
    public void hiUser(User user) {
        System.out.println(user);
    }

}

客户端动态代理,调用代码:

/**
     * 动态客户端调用,利用反射不需要引入服务端Service类
     * @throws Exception 
     */
    @Test
    public void testCXFClient_2() throws Exception{
        try{
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); 
        URL url= new URL("http://localhost:8888/cxf/webservice/HelloWorld?wsdl");
        Client client = dcf.createClient(url);   
        client.invoke("sayHi", "xbz");
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

使用CXF的动态客户端代理调用webservice,出现异常:
No operation was found with the name

问题出现原因如下描述:

    使用动态客户端调用webservice时,需要webservice的接口类与服务实现类的namespace保持一致。
     体现在wsdl文件比较容易看出
CXF异常:No operation was found with the name_第1张图片


解决方案:(2选1)

1.修改接口类与实现类的包位置,保持在同包下。
2.修改实现类的@Webservice注解,添加属性targetNamsespace,使得namespace保持一致。如下:


@WebService(
        endpointInterface="com.ainoties.core.webservice.sample.HelloCXF",
        serviceName="helloworld",
    targetNamespace="http://sample.webservice.core.ainoties.com/"
)

如何确定namespace:
CXF异常:No operation was found with the name_第2张图片
最后确定namespace一致,还是要通过wsdl文件确定:
CXF异常:No operation was found with the name_第3张图片

你可能感兴趣的:(CXF)