boot+cxf,实现webservice

简介: 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

20210107144414436.png


文件内容:

<?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

可以看到:

20210107145229496.png


根据 endpoint address 和方法名查看:

输入: http://192.168.0.168:8080/cxf/xingYeBank/getAllData?wsdl

可以看到:


20210107145452829.png


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


执行结果:

20210107145727702.png

相关文章
|
2月前
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
|
2月前
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
|
2月前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
|
2月前
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
|
2月前
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
|
2月前
|
Linux Python
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
|
2月前
|
存储 安全 网络安全
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
|
2月前
|
存储 Linux 网络安全
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
|
2月前
【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
|
2月前
|
Web App开发 安全 JavaScript
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
下一篇
无影云桌面