(五)WebService之CXF和Spring整合

  • 引言
  • 项目实现
    • 1POMxml文件
    • 2webxml文件
    • 3soap接口实现类实体类
    • 4rest接口实现类实体类
    • 5spring和cxf配置文件
    • 6验证服务是否发布成功
  • 代码下载

1.引言

      在目前的JavaWeb开发过程中,Spring有着至关重要的地位,所以也就存在着需求:CXF与Spring的整合,CXF和Spring是可以无缝整合的,在本篇博客中就是要用maven实现spring与CXF的整合。我们最终要完成的项目目录如下图所示:

(五)WebService之CXF和Spring整合_第1张图片

所实现的功能有:

  • 将Person,PersonService,PersonServiceImpl发布为Rest类型的服务
  • 将User ,UserService,UserServiceImpl发布为Soap类型的服务
  • 允许我们的Web服务可以跨域请求

2.项目实现

2.1POM.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.0modelVersion>
  <groupId>springgroupId>
  <artifactId>cxfartifactId>
  <packaging>warpackaging>
  <version>1.0-SNAPSHOTversion>
  <name>cxf Maven Webappname>
  <url>http://maven.apache.orgurl>
  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <cxf.version>3.1.9cxf.version>
    <jackson.version>1.9.13jackson.version>
    <spring.version>4.1.9.RELEASEspring.version>
  properties>
  <dependencies>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${spring.version}version>
    dependency>
    
    
    <dependency>
      <groupId>org.apache.cxfgroupId>
      <artifactId>cxf-rt-frontend-jaxwsartifactId>
      <version>${cxf.version}version>
    dependency>
    
    <dependency>
      <groupId>org.apache.cxfgroupId>
      <artifactId>cxf-rt-frontend-jaxrsartifactId>
      <version>${cxf.version}version>
    dependency>
    <dependency>
      <groupId>org.apache.cxfgroupId>
      <artifactId>cxf-rt-transports-httpartifactId>
      <version>${cxf.version}version>
    dependency>
    
    <dependency>
      <groupId>com.thetransactioncompanygroupId>
      <artifactId>java-property-utilsartifactId>
      <version>1.9version>
    dependency>
    <dependency>
      <groupId>com.thetransactioncompanygroupId>
      <artifactId>cors-filterartifactId>
      <version>1.7version>
    dependency>
    
    <dependency>
      <groupId>org.codehaus.jacksongroupId>
      <artifactId>jackson-jaxrsartifactId>
      <version>${jackson.version}version>
    dependency>
    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.9version>
      <scope>testscope>
    dependency>
  dependencies>
  <build>
    <finalName>cxffinalName>
  build>
project>

2.2web.xml文件



<web-app>
    <display-name>Archetype Created Web Applicationdisplay-name>
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring.xmlparam-value>
    context-param>
    
    <filter>
        <filter-name>CORSfilter-name>
        <filter-class>com.thetransactioncompany.cors.CORSFilterfilter-class>
        
        <init-param>
            <param-name>cors.allowOriginparam-name>
            <param-value>*param-value>
        init-param>
        <init-param>
            <param-name>cors.supportedMethodsparam-name>
            <param-value>GET, POST, HEAD, PUT, DELETEparam-value>
        init-param>
        <init-param>
            <param-name>cors.supportedHeadersparam-name>
            <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modifiedparam-value>
        init-param>
        <init-param>
            <param-name>cors.exposedHeadersparam-name>
            <param-value>Set-Cookieparam-value>
        init-param>
        <init-param>
            <param-name>cors.supportsCredentialsparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    <filter-mapping>
        <filter-name>CORSfilter-name>
        
        <url-pattern>/ws/*url-pattern>
    filter-mapping>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <servlet>
        <servlet-name>cxfservlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>cxfservlet-name>
        <url-pattern>/ws/*url-pattern>
    servlet-mapping>
    
web-app>

2.3soap接口,实现类,实体类

  • User实体类
package com.skd.domain;


public class User {
    private String username;
    private String password;

    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;
    }
}
  • UserService接口
package com.skd.service;

import com.skd.domain.User;

import javax.jws.WebService;

@WebService
public interface UserService {
    public User getUser(String name);
}
  • 接口实现类
package com.skd.service.impl;

import com.skd.domain.User;
import com.skd.service.UserService;
public class UserServiceImpl implements UserService{
    public User getUser(String name) {
        User u=new User();
        u.setUsername(name);
        u.setPassword("123456");
        return u;
    }
}

2.4rest接口,实现类,实体类

  • 实体类
package com.skd.domain;


import java.io.Serializable;

public class Person implements Serializable{
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
  • 接口
package com.skd.service;

import com.skd.domain.Person;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
@Produces( {MediaType.APPLICATION_JSON })
public interface PersonService {
    @POST
    @Path("/person/{name}")
    public Person getPerson(@PathParam("name") String name);
    @POST
    @Path("/persons")
    public List getPersons();
}
  • 接口实现类
package com.skd.service.impl;

import com.skd.domain.Person;
import com.skd.service.PersonService;

import java.util.ArrayList;
import java.util.List;

public class PersonServiceImpl implements PersonService{
    public Person getPerson(String name) {
        Person p=new Person();
        p.setName(name);
        p.setAge(100);
        return p;
    }

    public List getPersons() {
        List persons=new ArrayList();
        Person p1=new Person();
        p1.setName("wpx");
        p1.setAge(100);
        persons.add(p1);
        Person p2=new Person();
        p2.setName("zlr");
        p2.setAge(100);
        persons.add(p2);
        return persons;
    }
}

2.5spring和cxf配置文件

  • CXF.xml

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd
       http://cxf.apache.org/jaxrs
       http://cxf.apache.org/schemas/jaxrs.xsd">
    
    <jaxws:server id="soapuser" address="/user"
                  serviceClass="com.skd.service.UserService">
        <jaxws:serviceBean>
            <bean class="com.skd.service.impl.UserServiceImpl" />
        jaxws:serviceBean>
    jaxws:server>
    
    <bean id="personServiceImpl" class="com.skd.service.impl.PersonServiceImpl">bean>
    <jaxrs:server id="personService" address="/rest">
        <jaxrs:serviceBeans>
            <ref bean="personServiceImpl" />
        jaxrs:serviceBeans>
        <jaxrs:providers>
            
            <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">bean>
        jaxrs:providers>
        <jaxrs:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor">bean>
         jaxrs:inInterceptors>
         <jaxrs:outInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor">bean>
        jaxrs:outInterceptors>
    jaxrs:server>
beans>

  • spring

<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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

        <import resource="cxf.xml">import>
beans>

2.6验证服务是否发布成功

访问地址http://localhost:8080/ws查看到下图便发布成功:

(五)WebService之CXF和Spring整合_第2张图片

3.代码下载

CXF和Spring整合代码下载地址为:CXF和Spring整合

你可能感兴趣的:(WebService)