添加依赖
<!--web service cxf框架依赖 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>4.0.2</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-xjc</artifactId> <version>4.0.2</version> </dependency> <!--web service cxf框架依赖 -->
具体代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import javax.xml.namespace.QName;
/**
* @ClassName:JaxWsUtil
* @Description:webservice客户端:
* 该类提供动态调用webservice服务
*/
@Slf4j
public class JaxWsUtil {
public static void main(String[] args) {
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("mobileCode", "1581076854");
// jsonObject.put("userID", "");
Object[] params = new Object[] {"1581076854",""};
String methodName = "getMobileCodeInfo";
JaxWsUtil.sendWsdl(methodName,params);
}
/**
* 2:动态调用
*/
public static void sendWsdl(String methodName,Object[] params) {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
QName qa = new QName("http://WebXml.com.cn/",methodName);
// objects = client.invoke(qa,jsonObject.toJSONString());
objects = client.invoke(qa,params);
final ObjectMapper mapper = new ObjectMapper();
log.info("webService返回 mapper is :{}",mapper.writeValueAsString(objects[0]));
log.info("webService返回 objects is :{}",JSON.toJSONString(objects));
} catch (java.lang.Exception e) {
log.info("webService返回异常 is :{}",e.getMessage());
}
}
}