boot+cxf,实现webservice

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);
    }

}

执行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值