码迷,mamicode.com
首页 > 编程语言 > 详细

Spring与apache CXF结合实例

时间:2014-09-04 18:18:39      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   java   ar   for   

本文的前提是已经有一个Spring的项目,在此基础上如何跟apache CXF进行结合,开发出WebService服务和调用WebService服务。

1.开发WebService

  1.引入jar包

  下载最新的jar包,并引入:\apache-cxf-3.0.1\lib\*(当然里面有些是不必要的,有兴趣的可以自己删减)。

  2.修改web.xml

<servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

  3.引入单独的spring配置文件cxf-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
    license agreements. See the NOTICE file distributed with this work for additional 
    information regarding copyright ownership. The ASF licenses this file to 
    you under the Apache License, Version 2.0 (the "License"); you may not use 
    this file except in compliance with the License. You may obtain a copy of 
    the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. -->
<!-- START SNIPPET: beans -->
<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">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <jaxws:endpoint id="UserService"
        implementor="cn.telchina.standard.service.user.UserServiceImpl" address="/UserService" />
</beans>
<!-- END SNIPPET: beans -->

上述代码中:

    jaxsw:endpoint是关键。

在web.xml中加入配置

bubuko.com,布布扣

  4.开发服务接口和服务实现类

接口类:

package cn.telchina.standard.service.user;

import javax.jws.WebService;

/**
 * 必须要有接口
 * @author Administrator
 *
 */
@WebService
public interface UserService {
    
    public boolean updateTheUser(String user);
    
    public boolean updateUsersCode(String user);
    
}

接口实现类:

package cn.telchina.standard.service.user;

import javax.jws.WebService;


@WebService(endpointInterface = "cn.telchina.standard.service.user.UserService")
public class UserServiceImpl implements UserService {

    @Override
    public boolean updateTheUser(String user) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean updateUsersCode(String user) {
        // TODO Auto-generated method stub
        return false;
    }

}

 

此时发布程序即可:

http://localhost:8080/cxfProject/services

bubuko.com,布布扣

http://localhost:8080/cxfProject/services/UserService?wsdl

bubuko.com,布布扣

2.调用WebService

调用CXF服务的方式有三种:

1.配置spring bean的方式

   新增配置文件:client-beans,并在web.xml中配置。

bubuko.com,布布扣

Client-beans.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:oxm="http://www.springframework.org/schema/oxm" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/oxm 
    http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd 
    http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd"> 
    
        <jaxws:client id="client"
        address="http://localhost:8080/cxfProject/services/UserService"
        serviceClass="cn.telchina.standard.service.user.UserService" />
</beans>

java代码:

public static void invokeBySpring(){
        // TODO Auto-generated method stub
                ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                        new String[] { "client-beans.xml" });

                UserService client = (UserService) context.getBean("client");

                boolean response = client.updateTheUser("Joe");
                System.out.println("Response: " + response);
    }

2.CXF的方式

public static void invokeService(){
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
            factory.setServiceClass(UserService.class);  
            factory.setAddress("http://localhost:8080/cxfProject/services/UserService");  
            UserService service = (UserService) factory.create();  
            
            boolean response = service.updateTheUser("Joe");
            System.out.println("#############"+response+"##############"); 
    }

3.RPC方式

public static void invokeService2() throws Exception{
         //这个是用cxf 客户端访问cxf部署的webservice服务  
        //千万记住,访问cxf的webservice必须加上namespace ,否则通不过  
        //现在又另外一个问题,传递过去的参数服务端接收不到  
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
        org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/cxfProject/services/UserService?wsdl");  
        //url为调用webService的wsdl地址  
        QName name=new QName("http://user.service.standard.telchina.cn/","updateTheUser");  
        //namespace是命名空间,methodName是方法名  
        String xmlStr = "name";  
        //paramvalue为参数值  
        Object[] objects=client.invoke(name,xmlStr);   
        //调用web Service//输出调用结果  
        System.out.println(objects[0].toString()); 
    }

 

PS:

   cxf的类库和axis2的类库不能放到一个项目中,否者会报错:

Invocation of init method failed; nested exception is java.lang.NoSuchFieldError: REFLECTION

Spring与apache CXF结合实例

标签:style   blog   http   color   os   io   java   ar   for   

原文地址:http://www.cnblogs.com/sdjnzqr/p/3956452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!