1.导入 cxf 依赖
<!--cxf-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.6</version>
</dependency>
2.在启动类中添加注解
@SpringBootApplication
@MapperScan("com.gtmap.fundsupervision.mapper")
@ImportResource(locations = {
"classpath:cxf/cxf.xml" })
public class FundSupervisionApplication {
public static void main(String[] args) {
SpringApplication.run(FundSupervisionApplication.class, args);
}
}
3.在 resources 下创建 cxf.xml
文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">
<!-- 将Bean托管给Spring -->
<bean id="xingYeBankWebService" class="com.gtmap.fundsupervision.webservice.XingYeBankWebServiceImpl">
</bean>
<!-- <jaxws:server address="/xingYeBank">-->
<!-- <jaxws:serviceBean>-->
<!-- <ref bean="xingYeBankWebService"/>-->
<!-- </jaxws:serviceBean>-->
<!-- </jaxws:server>-->
</beans>
4.创建 cxf 配置类
@Configuration
public class CxfConfig {
@Bean
public ServletRegistrationBean newServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* @return
*/
@Bean
@Qualifier("xingYeBankWebServiceImpl") // 注入webService
public Endpoint endpoint(XingYeBankWebServiceImpl xingYeBankWebServiceImpl) {
EndpointImpl endpoint = new EndpointImpl(springBus(), xingYeBankWebServiceImpl);
endpoint.publish("/xingYeBank");// 暴露webService api,用在资源访问
return endpoint;
}
}
5.创建 webservice 接口
@WebService
public interface XingYeBankWebService {
//获取全部协议的数据
@WebMethod
List<XingYeBankZjjgxyDto> getAllData();
}
6.创建接口实现类
@Service
@WebService(endpointInterface="com.gtmap.fundsupervision.webservice.XingYeBankWebService") //webservice接口全类名
public class XingYeBankWebServiceImpl implements XingYeBankWebService {
//spring使用
@Autowired
private FcjyClfZjjgxyMapper fcjyClfZjjgxyMapper;
/**
* 获取全部协议的数据
* @return
*/
@Override
public List<XingYeBankZjjgxyDto> getAllData() {
//业务代码
}
}
7.启动项目,输入网址查看是否发布成功
输入:http://localhost:8080/cxf
可以看到:
根据 endpoint address 和方法名查看:
输入: http://192.168.0.168:8080/cxf/xingYeBank/getAllData?wsdl
可以看到:
8.自定义一个client,去调用方法:
public class XingYeBankClient {
public static void main(String[] args) throws Exception {
String wsdl = "http://192.168.0.168:8080/cxf/xingYeBank/getAllData?wsdl";
String TargetNamespace = "http://webservice.fundsupervision.gtmap.com/";
String methodName = "getAllData";
//创建动态客户端
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient(wsdl);
//客户端执行方法
Object[] invoke = client.invoke(new QName(TargetNamespace, methodName));
System.out.println("结果:" + invoke);
}
}
执行结果:
扫描二维码关注公众号,回复:
12464097 查看本文章
