• 使用CXF开发JAX-WS类型的WebService


    使用CXF记得要先加入CXF的jar包

    方法1

    Cxf编程实现:

    • 使用jaxwsServerFactoryBean发布 webservice服务端。

    需要设置:

    1. jaxwsServerFactoryBean.setAddress(“webservice地址”)
    2. jaxwsServerFactoryBean.setServiceClass(“porttype类路径”);//由程序员编写的
    3. jaxwsServerFactoryBean.setServiceBean(“porttype类对象”);
    4. jaxwsServerFactoryBean.create();//发布一个服务
    public class WeatherServer {
        
        public static void main(String[] args) {
            
            //使用jaxWs发布soap协议的webservice
            JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
            //指定 webservice地址
            jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weather");
            //指定 porttype
            jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class);
            
            //指定服务类对象
            jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl());
            
            //发布服务
            jaxWsServerFactoryBean.create();
            
        }
    
    }

    使用jaxwsProxyFactoryBean实现客户端调用webservice服务

    1. jaxwsServerFactoryBean.setAddress(“webservicewsdl地址”)
    2. jaxwsServerFactoryBean.setServiceClass(“porttype类路径”);//portType是由wsdl2java工具生成
    3. jaxwsServerFactoryBean.setCreate();//创建porttype对象
    public class WeatherClient {
    
        public static void main(String[] args) {
            // jaxWsProxyFactoryBean调用webservice服务端
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 调用地址
            jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:12345/weather?wsdl");
            // 设置portType
            jaxWsProxyFactoryBean.setServiceClass(WeatherInterface.class);
            // 创建portType
            WeatherInterface weatherInterface = (WeatherInterface) jaxWsProxyFactoryBean
                    .create();
    
            // 调用portType方法
    
            List<WeatherModel> list = weatherInterface.queryWeather("郑州");
    
            for (WeatherModel weatherModel : list) {
                System.out.println(weatherModel.getDetail());
                Date date = weatherModel.getData().toGregorianCalendar().getTime();
                System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
                System.out.println(weatherModel.getTemperatureMax());
                System.out.println(weatherModel.getTemperatureMin());
            }
    
        }
    
    }

    方法2

    Cxfspring整合开发服务端和客户端。

    • 使用<jaxws:Server 发布 webservice服务端

      在<jaxws:Server设置  AddressserviceClassserviceBean

    <!-- service -->
    <bean id="weatherInterface" class="cn.allan.ws.service.WeatherInterfaceImpl"/>
    
    <!-- 发布服务
    使用jaxws:server和jaxws:endpoint可以发布服务
    webservice地址=tomcat地址+cxf servlet的路径+/weather
     -->
    <jaxws:server address="/weather" serviceClass="cn.allan.ws.service.WeatherInterface">
        <jaxws:serviceBean>
            <ref bean="weatherInterface"/>
        </jaxws:serviceBean>
    </jaxws:server>

    接口:

    @BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)表示基于soap1.2协议,不写是1.1
    @WebService(
            targetNamespace="http://weather.allan.cn/",//指定 wsdl的命名空间
            name="WeatherInterface",//指定portType的名称
            portName="WeatherInterfacePort",//指定port的名称
            serviceName="WeatherService"//服务视图的名称
            //endpointInterface="cn.allan.ws.service.WeatherInterface2"//指定哪个接口中方法要发布成webservice服务,接口中加上@webservice注解
            )
    @BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
    public interface WeatherInterface {
        
        //查询三天天气
        public @WebResult(name="result") List<WeatherModel> queryWeather(@WebParam(name="cityName") String cityName);
    
    }
    • 使用<jaxws:Client 调用webservice服务

           <jaxws:Client设置  AddressserviceClass

    <!-- 使用<jaxws:client调用服务端
    jaxws:client内部使用JaxWsProxyFactoryBean方式
    serviceClass:指定portType地址(需要使用wsdl2java工具生成)
     -->
    <jaxws:client id="weatherClient" address="http://localhost:8080/ws_1231_cxf_spring_server/ws/weather?wsdl"
    
    serviceClass="cn.allan.weather.WeatherInterface">
      
    </jaxws:client>

    测试客户端

    public class ClientTest {
    
        private ApplicationContext applicationContext;
    
        @Before
        public void before() {
    
            applicationContext = new ClassPathXmlApplicationContext(
                    "applicationContext.xml");
    
        }
    
        @Test
        public void testCxfSpringClient() {
            // 从spring容器中取出porttype
            WeatherInterface weatherInterface = (WeatherInterface) applicationContext
                    .getBean("weatherClient");
    
            // 调用portType的方法
            List<WeatherModel> list = weatherInterface.queryWeather("郑州");
    
            for (WeatherModel weatherModel : list) {
                System.out.println(weatherModel.getDetail());
                Date date = weatherModel.getData().toGregorianCalendar().getTime();
                System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
                System.out.println(weatherModel.getTemperatureMax());
                System.out.println(weatherModel.getTemperatureMin());
            }
        }
    
    }

    web.xml配置

        <!-- 加载 spring容器 -->
         <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
        </context-param>
        <listener>
          <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
    
        <!-- cxf的servlet -->
        <servlet>
            <servlet-name>cxf</servlet-name>
            <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <!-- 本系统webservice的路径必须以/ws/开头 -->
        <servlet-mapping>
            <servlet-name>cxf</servlet-name>
            <url-pattern>/ws/*</url-pattern>
        </servlet-mapping>
  • 相关阅读:
    C++模板类报错:函数未定义
    linux top命令和sar命令
    XAMPP的服务在CentOS中无法启动
    XAMPP安装LAMP后无法使用数据库
    关于try catch语句中finally代码块有没有必要的思考
    eclipse卡死的两种解决办法
    VS2019 配置 OpenCV-4.4.0
    VS2019+OpenCV4配置报错:模块计算机类型x64与目标计算机类型x86冲突
    slide js用法 公告 广告 新闻滚动
    input type:text输入框点击输入,文字消失
  • 原文地址:https://www.cnblogs.com/laoyeye/p/6517928.html
Copyright © 2020-2023  润新知