webservice发布服务:CXF及客户端调用

Posted super超人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webservice发布服务:CXF及客户端调用相关的知识,希望对你有一定的参考价值。

2.CXF:(与spring整合)

CXF相对来说操作没有AXIS繁琐

1.导入spring的jar包和cxf的jar包

2.在spring的核心配置文件中配置发布的接口类

<?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"
 xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
     <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
 <import resource="classpath:META-INF/cxf/cxf.xml" />
 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
     <!-- 
       通过配置文件发布一个不带接口的webservice
       1:id
       2:提供服务的访问地址
       3:提供服务的类...
      -->
 <jaxws:endpoint id="springService" address="/springService" implementor="cn.itcast.cxf.web.SpringServiceHello">
 </jaxws:endpoint>
 
 <!-- 我的天气预报 -->
 <!-- <jaxrs: id="weather" address="/weather" serviceClass="cn.itcast.cxf.web.Weather">
 
 </jaxrs:server> -->
 <jaxws:endpoint id="weather" address="/weather" implementor="cn.itcast.cxf.web.Weather">
 </jaxws:endpoint>
 <!-- 音乐人 -->
 <jaxws:endpoint id="music" address="/music" implementor="cn.itcast.cxf.web.Music">
 </jaxws:endpoint>
 
 
 <!-- 发布一个带接口的webservice 
      1:id
      2:提供服务的访问地址
      3:接口的类型
 -->
 
 <jaxws:server id="ipaddress" address="/ipaddress" serviceClass="cn.itcast.cxf.web.IpAddressService">
   <jaxws:serviceBean>
    <!-- 接口的实现类... -->
    <bean class="cn.itcast.cxf.web.IpAddressServiceImpl"></bean>
   </jaxws:serviceBean>
   <!-- 请求的消息拦截器 -->
   <!-- <jaxws:inInterceptors>
    <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
   </jaxws:inInterceptors> -->
   <!-- 响应的消息拦截器... -->
   <!-- <jaxws:outInterceptors>
   <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
   </jaxws:outInterceptors> -->
 </jaxws:server>
 
 
 
</beans>

3.在web.xml中配置cxf的servlet过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <!-- 通过spring 的listener 去解析 cxf-Servlet 配置文件... -->
 <!-- <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener> -->
 
 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/cxf-servlet.xml</param-value>
 </context-param>
 
 
 <!--
 
   第一次访问CXFServlet 然后/WEB-INF/cxf-servlet.xml 交给 init 方法 去解析...
  cxf 与spring 进行无缝的整合,调用spring.jar 包里面的类... 解析 cxf-servlet.xml 配置文件...拿到配置文件里面的信息..
   <jaxws:server id="ipaddress" address="/ipaddress" serviceClass="cn.itcast.cxf.web.IpAddressService">
   
   <jaxws:serviceBean>
    接口的实现类...
    <bean class="cn.itcast.cxf.web.IpAddressServiceImpl"></bean> 
 
  JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean();
  request.
  
  http://localhost:8080/cxfspringweb/services  + /ipaddress
  
  bean.setAddress(http://localhost:8080/cxfspringweb/services  + /ipaddress);
  
  //接口
  通过反射拿到cn.itcast.cxf.web.IpAddressService.class
  bean.setServiceClass();
  
  bean.ServiceBean(Class.forName("cn.itcast.cxf.web.IpAddressServiceImpl").newInstance());
  
  bean.create();
 
 
  -->
 
 <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <init-param>
         <param-name>config-location</param-name>
         <param-value>/WEB-INF/cxf-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
 
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
启动服务器,访问wsdl文档。如果访问成功说明接口发布成功

客户端代码生成:

使用jdk命令wsimport -s . -p cn.itcast http://localhost:8080/axis2Server/services/testService?wsdl生成客户端代码,也可将此语句放置在自定义的bat文件中执行

客户端代码调用:(通过配置文件调用)

新建beanssss.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"
 xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
       <!-- 通过配置文件调用... 通过这种方式调用需要依赖一个接口... -->
       <jaxws:client id="itcast" address="http://192.168.9.12:8080/cxfspringweb/services/ipaddress?wsdl" serviceClass="com.baidu.config.IpAddressService">
       </jaxws:client>
       
       <jaxws:client id="weather" address="http://192.168.9.12:8080/cxfspringweb/services/weather?wsdl" serviceClass="cn.itcast.weather.Weather">
       </jaxws:client>
       
       <jaxws:client id="music" address="http://192.168.9.12:8080/cxfspringweb/services/music?wsdl" serviceClass="cn.itcast.music.Music">
       </jaxws:client>

</beans>

通过bean的方式调用:

package cn.itcast.spring.web;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.music.Music;
import cn.itcast.weather.Weather;

import com.baidu.config.IpAddressService;

/**
 * 
 * 通过配置文件来调用webservice 服务...
 * @author Administrator
 *
 */
public class SpringConfigInvoke {
 /*public Weather weather;
 
 public Weather getWeather() {
  return weather;
 }

 public void setWeather(Weather weather) {
  this.weather = weather;
 }*/
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  ApplicationContext context=new ClassPathXmlApplicationContext("beanssss.xml");
  //根据IP查询所在地
  IpAddressService addressService=(IpAddressService) context.getBean("itcast");
  String ipaddress=addressService.getAddressByIp("192.168.9.12");
  System.out.println(ipaddress);
  
  //我的天气
  /*SpringConfigInvoke springConfigInvoke = new SpringConfigInvoke();
  springConfigInvoke.weather();*/
  
  Weather weather = (Weather) context.getBean("weather");
  System.out.println(weather.bjWeather());
  
  //音乐人
  Music music = (Music) context.getBean("music");
  System.out.println(music.singer());
 }
 
 /*public void weather(){
   weather.bjWeather();
 }*/

}

 

以上是关于webservice发布服务:CXF及客户端调用的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot | 第三十四章:CXF构建WebService服务

如何使用CXF调用webservice接口

webservice -- cxf客户端调用axis2服务端

cxf如何创建webservice客户端

CXF Webservice 服务器调用 Axis1 Webservice 作为客户端问题(无法找到有效的 EngineConfigurationFactory)

CXF方式搭建本地webservice服务和soap方式调用踩坑