在使用如下CXF动态的时候,创建客户端的时候报错No operation was found with the name
客户端
package com.p.dynamic.client;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
*/
public final class ComplexClient {
public static void main(String[] args) throws Exception {
QName SERVICE_NAME = new QName("http://com.p.service/", "sayHi");
String wsdlURL = "http://localhost:9000/Complex?wsdl";
//JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
//Client client = factory.createClient(wsdlURL,SERVICE_NAME ); //报错
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient(wsdlURL);
Object[] result = client.invoke(SERVICE_NAME,"调用大数据"); //报错
}
}
在客户端启动的时候报错No operation was found with the name。检查原因是因为CXF默认取的是接口类所在路径作为命名空间,一般问题出在接口上,下面是接口
接口
package com.p.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHi(String text);
}
看到因为,因为在client动态调用的时候, QName SERVICE_NAME = new QName("http://com.p.service/", "sayHi"); 使用的是
http://com.p.service/
而正确的应该是,
“http://service.p.com/”
。为了出现其他问题,应该再添加
endpointInterface="com.yuepai.service.HelloWorld",
targetNamespace="http://com.yuepai.service"
如下:
package com.yuepai.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.w3c.dom.Document;
@WebService(
endpointInterface="com.p.service.HelloWorld",
targetNamespace="http://com.p.service")
public interface HelloWorld {
@WebMethod
String sayHi(String text);
}
启动服务
package com.yuepai.dynamic.server;
import javax.xml.ws.Endpoint;
import com.p.service.impl.HelloWorldImpl;
public class Server {
protected Server() throws Exception {
System.out.println("Starting Server");
HelloWorldImpl complexImpl = new HelloWorldImpl();
String address = "http://localhost:9000/Complex";
Endpoint.publish(address, complexImpl);
}
public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
实现类
package com.p.service.impl;
import com.p.service.HelloWorld;
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
}