服务提供方
- 引入依赖
<!-- cxf框架依赖 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.12</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.12</version>
</dependency>
2.暴露接口
package com.his.uc.facade;
@WebService
(name = "IDepartFacade",//暴露服务名称
targetNamespace = "http://facade.uc.his.com/")//接口包名倒置
public interface IDepartFacade {
@WebMethod
List<DepartOutput> list(@WebParam(name = "inputParam")DepartInput inputParam);
}
3.实现类
package com.his.api.facade.uc;
@Slf4j
@Service(interfaceName = "Depart")
@WebService(serviceName = "IDepartFacade",//与接口中指定的name一致com.his.server.service.uc.IDepartService
targetNamespace = "http://facade.uc.his.com/",//与接口中的命名空间一致,包名倒置
endpointInterface = "com.his.uc.facade.IDepartFacade")//接口的全限定名
public class DepartFacadeImpl extends ServiceImpl<DepartMapper, Depart> implements IDepartFacade {
@Override
public List<DepartOutput> list(DepartInput inputParam) {
Depart doObject = inputConvert.input2Do(inputParam);
List<Depart> result = iDepartService.list(doObject);
List<DepartOutput> departOutputs = outputConvert.DoList2OutputList(result);
if (inputParam.getServiceIdent()==null)
{
if (CollectionUtils.isNotEmpty(departOutputs)) {
departOutputs.forEach(departOutput -> {
DepartScopeInput departScopeInput = new DepartScopeInput();
departScopeInput.setDepartCode(departOutput.getDepartCode());
departOutput.setDepartScopeOutputs(departScopeFacade.list(departScopeInput));
});
}
}
return departOutputs;
}
}
4.CXF配置类
package com.his.server.util;
import com.his.api.facade.uc.DepartFacadeImpl;
import com.his.api.facade.uc.UserFacadeImpl;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class CxfWebServiceConfig {
@Autowired
DepartFacadeImpl departFacadeImpl;
@Autowired
UserFacadeImpl userFacadeImpl;
@Bean
public ServletRegistrationBean wsServlet(){
return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint syncEmpOrgImpl() {
EndpointImpl endpoint = new EndpointImpl(springBus(), departFacadeImpl);// 绑定要发布的服务实现类
EndpointImpl endpoint1 = new EndpointImpl(springBus(), userFacadeImpl);// 绑定要发布的服务实现类
//网上很多直接在这个地方 new userFacadeImpl();
//EndpointImpl endpoint1 = new EndpointImpl(springBus(), new userFacadeImpl());//这样写保证不会调用到userFacadeImpl的方法,原因:请自行研究spring底层实现
endpoint1.publish("/userList");
endpoint.publish("/testDepart"); // 接口访问地址
return endpoint;
}
}
访问:http://localhost:应用端口/ws/testDepart?wsdl (访问的是testDepart接口)
服务调用方
采用动态调用的方式
1.引入依赖
<!-- 当时测试的时候是产生了冲突,所以才这样写 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>${cxf-spring-boot-starter-jaxws.version}</version>
<exclusions>
<exclusion>
<artifactId>cxf-core</artifactId>
<groupId>org.apache.cxf</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.6</version>
</dependency>
${cxf-spring-boot-starter-jaxws.version} 版本为<cxf-spring-boot-starter-jaxws.version>3.2.5</cxf-spring-boot-starter-jaxws.version>
2.实现类调用
/**
*
*
* @Author generator@asiainfo.com
* @Date 2021-01-15
*/
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class DepartContrastServiceImpl extends BaseServiceImpl<DepartContrastMapper, DepartContrast> implements IDepartContrastService {
/**
* 调用webservice服务获取数据
*
* @return
*/
@Override
public List<Depart> list(Depart entity){
String wsdlUrl = "http://127.0.0.1:8181/ws/testDepart?wsdl";
//动态调用的客户端工厂类
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient(wsdlUrl);
//设置超时单位为毫秒
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(30000); //连接超时
httpClientPolicy.setAllowChunking(false); //取消块编码
httpClientPolicy.setReceiveTimeout(30000); //响应超时
http.setClient(httpClientPolicy);
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
// 需要密码的情况需要加上用户名和密码
client.getOutInterceptors().add(new ClientLoginInterceptor(UserInformation.getUserName(),UserInformation.getPassWord()));
List<Integer> hisdepartCodeList = departContrastMapper.selectGetHisdepartCodeList();
Object[] result = null;
Object[] objects = new Object[0];
List<Depart> departOutputList = null;
try {
Object departInput = Thread.currentThread().getContextClassLoader().loadClass("com.his.uc.facade.DepartInput").newInstance();//加载类为:服务端自定义对象命名空间+实体类名称
// invoke("方法名",参数1,参数2,参数3....);
Method m = departInput.getClass().getMethod("setDeleteYn", Integer.class);
Method m1 = departInput.getClass().getMethod("setOrgCode", Long.class);
m.invoke(departInput, 0);
m1.invoke(departInput, 36L);
//getHisList为调用的接口名称
objects = client.invoke("getHisList",departInput,hisdepartCodeList);
departOutputList = BeanConvertUtils.changeList(objects[0], Depart.class);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
return departOutputList;
}
}