导入依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>4.0.4</version>
</dependency>
一、webService接口发布
1.1 接口配置类
import com.ctyk.dism.service.DismWebService;
import jakarta.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Configuration
public class WebServiceConfig {
@Resource
private DismWebService dismWebService;
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean(name = "cxfServlet")
public ServletRegistrationBean<CXFServlet> cxfServlet() {
return new ServletRegistrationBean<>(new CXFServlet(), "/dism/webservice/*");
}
@Bean(name = "DismWebServiceEndpoint")
public Endpoint endpoint() {
// 参数二,是SEI实现类对象
EndpointImpl endpoint = new EndpointImpl(springBus(), dismWebService);
// 发布服务
endpoint.publish("/dism");
return endpoint;
}
}
1.2WebService接口
import com.ctyk.dism.exception.DismException;
import jakarta.jws.WebMethod;
import jakarta.jws.WebParam;
import jakarta.jws.WebService;
@WebService
public interface DismWebService {
@WebMethod
String getDocument(@WebParam(name = "methodName") String methodName, @WebParam(name = "body") String body) throws DismException;
}
1.3 WebService接口实现类
@Service
@WebService(name = "DismWebService", targetNamespace = "http://service.dism.ctyk.com/",
endpointInterface = "com.ctyk.dism.service.DismWebService")
@Slf4j
public class DismWebServiceImpl implements DismWebService {
@Override
public String getDocument(String methodName, String body) {
log.error("执行方法:" + methodName);
log.error("执行方法:" + body);
//具体的方法实现
return null;
}
}
1.4 测试webService接口(浏览器输入http://ip:12050/dism/webservice/dism?wsdl)
二、WebService接口的调用
2.1 调用接口工具类
package com.ctyk.igs.service.util;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import javax.xml.namespace.QName;
public class WebServiceClientUtil {
private static final JaxWsDynamicClientFactory DCF = JaxWsDynamicClientFactory.newInstance();
public static String callWebservice(String wsdUrl, String operationName, Object... params) throws Exception {
// 如果有命名空间需要加上这个,第一个参数为命名空间名称,第二个参数为WebService方法名称
// QName qName = new QName("http://service.dism.ctyk.com/", operationName);
Client client = DCF.createClient(wsdUrl);
// 如果需要认证,取消注释并适当配置
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] results = client.invoke(qName, params);
if (results != null && results.length > 0) {
return results[0].toString();
}
return null;
}
}
2.2 测试
public String getWebservice() throws Exception {
return WebServiceClientUtil.callWebservice("http://ip:12050/dism/webservice/dism?wsdl", "getDocument", methodName, body);
}
三、遇到的问题
3.1 Unable to create schema compiler
报错
解决方法:11以后的jdk没有jaxb,所以需要自行通过maven的方式来引入
# 引入依赖
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>4.0.4</version>
</dependency>
3.2 No operation was found with the name {http://service.dism.ctyk.com}getDocument.
解决方法:
1、在动态调用时增加命名空间设置
QName qName = new QName(“http://service.dism.ctyk.com/”, operationName);
2、注意观察命名空间是否正确。最后是缺少“/”,我就是缺少“/”才一直调不通