`

Spring整合CXF

阅读更多
1. Spring整合CXF之发布WebService服务

CXF官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-spring.html

用Spring来整合CXF,来发布WebService服务;
New Maven Project
-> maven-archetype-webapp 1.0
    Group Id: com.andrew.webservice
    Artfact Id: WS_Spring_CXF
    Version:0.0.1-SNAPSHOT
    Packaging:jar


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.andrew.webservice</groupId>
  <artifactId>WS_Spring_CXF</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>WS_Spring_CXF Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- 添加Spring支持 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-tx</artifactId>
         <version>4.1.7.RELEASE</version>
        </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- 添加cxf支持  -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.1.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.5</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>WS_Spring_CXF</finalName>
  </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WS_Spring_CXF</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
    <!-- Spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- Spring监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
     <servlet-name>CXFServlet</servlet-name>
     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
       <servlet-name>CXFServlet</servlet-name>
       <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>
</web-app>

MapAdapter.java

package com.andrew.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import com.andrew.entity.MyRole;
import com.andrew.entity.Role;

public class MapAdapter extends XmlAdapter<MyRole[], Map<String, List<Role>>> {

    @Override
    public Map<String, List<Role>> unmarshal(MyRole[] v) throws Exception {
        Map<String, List<Role>> map = new HashMap<String, List<Role>>();
        for (int i = 0; i < v.length; i++) {
            MyRole r = v[i];
            map.put(r.getKey(), r.getValue());
        }
        return map;
    }

    @Override
    public MyRole[] marshal(Map<String, List<Role>> v) throws Exception {
        MyRole[] roles = new MyRole[v.size()];
        int i = 0;
        for (String key : v.keySet()) {
            roles[i] = new MyRole();
            roles[i].setKey(key);
            roles[i].setValue(v.get(key));
            i++;
        }
        return roles;
    }
}

MyRole.java

package com.andrew.entity;

import java.util.List;

public class MyRole {
    private String key;
    private List<Role> value;
    
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public List<Role> getValue() {
        return value;
    }
    public void setValue(List<Role> value) {
        this.value = value;
    }
}

Role.java

package com.andrew.entity;

public class Role {
    private Integer id;
    private String roleName;
    public Role() {
        super();
    }
    public Role(Integer id, String roleName) {
        super();
        this.id = id;
        this.roleName = roleName;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
}

User.java

package com.andrew.entity;

public class User {
    private Integer id;
    private String userName;
    private String password;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

MyInterceptor.java

package com.andrew.interceptor;

import java.util.List;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class MyInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
    public MyInterceptor() {
        super(Phase.PRE_INVOKE);
    }
    @SuppressWarnings("null")
    public void handleMessage(SoapMessage message) throws Fault {
        List<Header> headers = message.getHeaders();
        if (headers == null && headers.size() == 0) {
            throw new Fault(new IllegalArgumentException("没有Header,拦截器实施拦截"));
        }
        Header firstHeader = headers.get(0);
        Element ele = (Element) firstHeader.getObject();
        NodeList uList = ele.getElementsByTagName("userName");
        NodeList pList = ele.getElementsByTagName("password");
        if (uList.getLength() != 1) {
            throw new Fault(new IllegalArgumentException("用户名格式不对"));
        }
        if (pList.getLength() != 1) {
            throw new Fault(new IllegalArgumentException("密码格式不对"));
        }
        String userName = uList.item(0).getTextContent();
        String password = pList.item(0).getTextContent();
        if (!userName.equals("andrew") || !password.equals("123456")) {
            throw new Fault(new IllegalArgumentException("用户名或者密码错误!"));
        }
    }
}

HelloWorld.java

package com.andrew.webservice;

import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.andrew.adapter.MapAdapter;
import com.andrew.entity.Role;
import com.andrew.entity.User;

@WebService
public interface HelloWorld {
    public String say(String str);
    public List<Role> getRoleByUser(User user);
    
    @XmlJavaTypeAdapter(MapAdapter.class)
    public Map<String,List<Role>> getRoles();
}

HelloWorldImpl.java

package com.andrew.webservice.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.andrew.entity.Role;
import com.andrew.entity.User;
import com.andrew.webservice.HelloWorld;

@Component("helloWorld")
@WebService
public class HelloWorldImpl implements HelloWorld {
    public String say(String str) {
        return "Hello:" + str;
    }
    public List<Role> getRoleByUser(User user) {
        List<Role> roleList = new ArrayList<Role>();
        if (user != null) {
            if (user.getUserName().equals("andrew") && user.getPassword().equals("123456")) {
                roleList.add(new Role(1, "经理"));
                roleList.add(new Role(2, "组长"));
            } else if (user.getUserName().equals("tony") && user.getPassword().equals("123456")) {
                roleList.add(new Role(3, "员工"));
            }
            return roleList;
        } else {
            return null;
        }
    }
    public Map<String, List<Role>> getRoles() {
        Map<String, List<Role>> map = new HashMap<String, List<Role>>();
        List<Role> roleList1 = new ArrayList<Role>();
        roleList1.add(new Role(1, "经理"));
        roleList1.add(new Role(2, "组长"));
        map.put("andrew", roleList1);
        List<Role> roleList2 = new ArrayList<Role>();
        roleList2.add(new Role(3, "员工"));
        map.put("jack", roleList2);
        return map;
    }
}

applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.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"/>
    
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.andrew.webservice" />
    
    <!-- 定义服务提供者  -->
    <jaxws:endpoint implementor="#helloWorld" address="/HelloWorld"></jaxws:endpoint>
</beans>

http://localhost:8080/WS_Spring_CXF/webservice/
http://localhost:8080/WS_Spring_CXF/webservice/HelloWorld?wsdl


2. Spring整合CXF之添加拦截器

applicationContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.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"/>
    
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.andrew.webservice" />
    
    <!-- 定义服务提供者  -->
    <jaxws:endpoint implementor="#helloWorld" address="/HelloWorld">
        <!-- 添加in拦截器 -->
        <jaxws:inInterceptors>
             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
             <bean class="com.andrew.interceptor.MyInterceptor"/>
        </jaxws:inInterceptors>
        <!-- 添加out拦截器 -->
        <jaxws:outInterceptors>
             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
        </jaxws:outInterceptors>
    </jaxws:endpoint>
</beans>
分享到:
评论

相关推荐

    Spring整合CXF发布服务

    当我们需要在Spring环境中发布Web服务时,Spring与CXF的整合就显得尤为重要。本篇文章将深入探讨如何实现Spring与CXF的整合,以便发布服务。 1. **Spring与CXF整合的基础** 在整合Spring和CXF之前,我们需要确保...

    Spring整合CXF demo

    **Spring整合CXF详解** Spring框架与Apache CXF的整合是企业级Java应用中常见的实践,主要用于构建基于SOAP和RESTful服务。这个"Spring整合CXF demo"项目提供了一个直观的例子,帮助开发者理解如何在Spring环境中...

    Spring集成Cxf调用WebServices

    总之,Spring集成CXF调用Web Services是一个强大的组合,它提供了灵活的配置方式和强大的功能,使开发者能够更高效地构建和维护Web服务应用。通过理解并实践上述知识点,开发者可以更好地驾驭这一技术栈。

    零配置spring 整合cxf

    标题 "零配置spring 整合cxf" 涉及的是在Spring框架中与Apache CXF服务集成,而无需繁琐的XML配置。Apache CXF是一个开源的Java框架,主要用于构建和开发服务导向架构(SOA)的应用程序。它支持多种Web服务标准,...

    spring整合CXF开发webService所需jar包

    - Spring整合CXF的相关库:cxf-spring-boot-starter-jaxws.jar、cxf-spring-boot-autoconfigure.jar等,这些jar文件帮助Spring容器自动配置CXF相关的bean。 - 其他依赖库:如wsdl4j.jar、jaxb-api.jar、jaxb-impl....

    Spring集成CXF实例(包含WSS4J安全认证)

    本实例将详细介绍如何在Spring环境中集成CXF,并使用WSS4J进行安全认证。 **一、Spring与CXF集成** 1. **配置CXFServlet**: 首先,我们需要在Spring的`web.xml`配置文件中声明CXF的Servlet,如`...

    Spring集成Cxf暴露WebServices示例

    本示例将探讨如何通过Spring集成Cxf来暴露Web服务,帮助开发者更好地理解和实现这一功能。 首先,我们需要了解Spring与Cxf的基本概念。Spring框架提供了一个全面的编程和配置模型,用于简化企业级Java应用程序的...

    spring集成cxf的ws与rs

    首先,让我们了解什么是Spring集成CXF。Spring框架提供了对CXF的支持,允许开发者在Spring容器中管理和配置CXF服务。这样,我们可以利用Spring的强大功能来管理CXF的服务生命周期,同时利用CXF的Web服务实现能力。 ...

    spring+cxf 整合jar包

    7. **测试支持**:Spring的测试框架可以很方便地对整合后的CXF服务进行单元测试和集成测试,确保服务的正确性。 在下载的"spring3+cxf2.7 整合jar包"中,可能包含了Spring 3.x版本和CXF 2.7版本的库文件,以及相关...

    spring集成cxf客户端和服务器端demo(含自定义拦截器)

    在本项目中,"spring集成cxf客户端和服务器端demo(含自定义拦截器)"是一个实战案例,展示了如何在Spring框架下与Apache CXF服务进行整合,实现客户端和服务端的交互,并利用拦截器来增强功能。以下是这个项目涉及的...

    零配置spring 整合cxf、struts2

    总结来说,"零配置spring 整合cxf、struts2"是一个旨在减少XML配置,通过注解驱动实现Spring、CXF和Struts2无缝集成的实践。通过这种方式,开发者可以专注于业务逻辑,提高开发效率,同时降低维护成本。在实际开发中...

    Spring整合CXF步骤,Spring实现webService,spring整合WebService

    下面我们将详细探讨Spring整合CXF的步骤以及如何在Spring中实现Web服务。 1. **环境准备**: 在开始整合之前,确保已安装JDK,并设置好环境变量。同时,你需要在项目中引入Spring和CXF的依赖库,通常通过Maven或...

    spring整合cxf服务

    在整合 Spring 和 CXF 之前,确保已安装了必要的软件,如 JDK、Maven 或者 Gradle 作为构建工具,以及集成开发环境(如 MyEclipse),并创建一个新的 Maven 或 Gradle 项目。 ### 2. 添加依赖 在项目的 `pom.xml` ...

    cxf整合spring

    将CXF与Spring整合的主要好处在于,可以利用Spring的强大管理和配置能力来管理CXF服务。Spring的DI可以帮助我们将CXF的服务bean与其他业务逻辑bean解耦,从而提高代码的可测试性和可维护性。此外,Spring还提供了对...

    spring集成cxf客户端和服务器端demo

    总之,“spring集成cxf客户端和服务器端demo”项目为我们提供了一个基础的框架,用于理解如何在Spring环境中使用CXF开发和消费Web服务。通过实践这个示例,开发者能够掌握这两款工具的整合技术,从而在实际项目中更...

    spring 集成 cxf 简单例子

    总结来说,Spring集成CXF的例子展示了如何在Spring环境下快速搭建一个Web服务,通过定义服务接口、实现接口、配置Spring和Web容器,最后通过测试验证服务是否正常运行。这个过程涉及到了Spring的bean管理和CXF的Web...

    spring集成cxf(webservice)

    ### Spring集成CXF(WebService) #### WebService概览 WebService是一种构建应用程序的普遍模型,能够跨平台、跨语言实现服务的交互与共享。它是一种自包含、自描述、模块化的应用,可以发布、定位并通过Web调用...

    cxf spring maven 实例

    5. **Spring整合CXF**:Spring可以通过`cxf-spring`模块来集成CXF,通过`&lt;jaxws:endpoint&gt;`或`&lt;jaxrs:server&gt;`标签在Spring配置文件中声明Web服务。这样,Spring容器会管理Web服务的生命周期,服务实例可以在需要时...

    CXF和spring整合所需jar包

    这里我们将深入探讨CXF与Spring整合时所需的jar包及其作用。 首先,我们需要理解整合的目的:通过Spring管理CXF的服务生命周期,使得服务的创建、配置和管理更加便捷。为了实现这一目标,我们需要以下关键的jar包:...

Global site tag (gtag.js) - Google Analytics