1. Phenomenon
Integrating the web service of Spring + CXF, the WSDL is successfully published, but an error is reported when calling
org.apache.cxf.common.i18n.uncheckedexception: no operation was found with
2. Solution 1
: add targetNamespace in the service interface
package com.gblfy.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(targetNamespace = "http://impl.service.gblfy.com/")
public interface IUserService {
@WebMethod
public String getCxf(@WebParam(name = "reqXml") String reqXml);
}
Implementation class
package com.gblfy.service.impl;
import com.gblfy.service.IUserService;
import javax.jws.WebService;
@WebService
public class UserServiceImpl implements IUserService {
@Override
public String getCxf(String reqXml) {
System.out.println("Message received:" + reqXml);
return "OK";
}
}
client
/**
* Single/multi-parameter calling tool class (Object type)
*
* @param cxfUrl url address
* @param method call method name
* @param reqXml send message body
* @return res return result
* @throws Exception If there is an exception, output the exception on the console and throw the exception
*/
public static String cxfClientParam(String cxfUrl, String method, Object... reqXml) throws Exception {
String res = null;
// Create a dynamic client
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(cxfUrl);
// If you need a password, you need to add a user name and password
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects = new Object[0];
try {
// Basic format: invoke("method name", parameter 1, parameter 2, parameter 3....);
objects = client.invoke(method, reqXml);
res = objects[0].toString();
System.out.println("Return data:" + res);
} catch (java.lang.Exception e) {
e.printStackTrace();
throw e;
}
return res;
}
3. Solution 2
Use QName and add the address of the service interface
package com.gblfy.service.client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;
import javax.xml.namespace.QName;
/**
* cxf client call (packaged inside the enterprise)
*
* @author gblfy
* @date 2021-09-17
*/
@Component
public class CxfClient {
public static void main(String[] args) throws Exception {
String cxfUrl = "http://127.0.0.1:8080/spring_cxf_war/webservice/userWS?wsdl";
String method = "getCxf";
String reqXml = "cxf request message";
//Call the service
CxfClient.cxfClientParam(cxfUrl, method, reqXml);
}
/**
* Single/multi-parameter calling tool class (Object type)
*
* @param cxfUrl url address
* @param method call method name
* @param reqXml send message body
* @return res return result
* @throws Exception If there is an exception, output the exception on the console and throw the exception
*/
public static String cxfClientParam(String cxfUrl, String method, String reqXml) throws Exception {
String res = null;
// Create a dynamic client
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(cxfUrl);
// If you need a password, you need to add a user name and password
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects = new Object[0];
try {
// Basic format: invoke("method name", parameter 1, parameter 2, parameter 3....);
QName qName = new QName("http://impl.service.gblfy.com/",method);
objects = client.invoke(qName, reqXml);
res = objects[0].toString();
System.out.println("Return data:" + res);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return res;
}
}