CXF集成Spring实现webservice的发布与请求

CXF集成Spring实现webservice的发布(服务端)

目录结构:

主要代码:

package com.cxf.spring.pojo;

public class User {

        int id ;
        String name = null;
        String address = null;
        String email = null;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }    

        @Override
        public String toString() {
            String re = id+":"+name+":"+address+":"+email+":";
            return re;
        }

}

package com.cxf.spring.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.cxf.spring.pojo.User;

@WebService
public interface IGreetingService {

    @WebMethod
    public String greeting(@WebParam(name ="name") String name);

    public User getUserByName(@WebParam(name = "name") String name);  

    public void setUser(@WebParam(name = "user") User user); 

}

package com.cxf.spring.service;

import com.cxf.spring.pojo.User;

public class GreetingServiceImpl implements IGreetingService{

    @Override
    public String greeting(String name) {

        return "HI:....."+name;
    }

    @Override
    public User getUserByName(String name) {
            User user = new User();
            user.setId(100);
            user.setName(name);
            user.setAddress("china");
            user.setEmail(name + "@test.com");
            return user;
    }

    @Override
    public void setUser(User user) {
        System.out.println("############Server setUser###########");
        System.out.println("setUser:" + user);  

    }

}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd">  

    <bean id="greetingServiceBean"
      class="com.cxf.spring.service.GreetingServiceImpl"></bean>

     <jaxws:server id="greetingService"
         serviceClass="com.cxf.spring.service.IGreetingService" address="/greet">
        <jaxws:serviceBean>
            <ref bean="greetingServiceBean"/>
        </jaxws:serviceBean>
    </jaxws:server>  

</beans>

<?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_3_0.xsd"
    version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
         <param-value>classpath:beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

       <servlet>
            <servlet-name>CXFService</servlet-name>
            <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>CXFService</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>

      <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>

</web-app>

CXF集成Spring实现webservice的请求(客户端)

主要代码:

package com.cxf.spring.pojo;

public class User {

        int id ;
        String name = null;
        String address = null;
        String email = null;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }        

    /*    @Override
        public String toString() {
            String re = id+":"+name+":"+address+":"+email+":";
            return re;
        }*/
}

package com.cxf.spring.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.cxf.spring.pojo.User;

@WebService
public interface IGreetingService {

    @WebMethod
    public String greeting(@WebParam(name ="name") String name);

    public User getUserByName(@WebParam(name = "name") String name);  

    public void setUser(@WebParam(name = "user") User user); 

}

package com.cxf.spring.service;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ServletToBeanProxy extends GenericServlet {

    private static final long serialVersionUID = -6841906526208019964L;

    private String targetBean;

    private Servlet proxy;

    @Override
    public void init() throws ServletException {
        super.init();
        WebApplicationContext wac = WebApplicationContextUtils
                .getRequiredWebApplicationContext(getServletContext());

        this.targetBean = getServletName();
        this.proxy = (Servlet) wac.getBean(targetBean);
        proxy.init(getServletConfig());
    }

    @Override
    public void service(ServletRequest request, ServletResponse response)
            throws ServletException, IOException {
        proxy.service(request, response);
    }
}

package com.cxf.spring.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.cxf.spring.pojo.User;

public class ClientServlet extends HttpServlet{

    private static final long serialVersionUID = 8534382443557539155L;

    @Autowired
    public IGreetingService client;

     @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            System.out.println("Yeah, it‘s a get test");

            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("Hello World, Getter");

            System.out.println("#############Client getUserByName##############");  

            User user = client.getUserByName("hoojo");

            System.out.println("greet=====:"+client.greeting("hoojo"));

            System.out.println(user);  

            user.setAddress("China-Guangzhou");
            client.setUser(user);  

          /*      PrintWriter out = response.getWriter();
                out.println("Hello,Spring.Servlet");
                response.sendRedirect("get.jsp");*/

        }

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            System.out.println("Yeah, it‘s a post test");

            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("Hello World, Poster");
           //  PrintWriter out = response.getWriter();
           //  out.println("Hello,Spring.Servlet");
           //  response.sendRedirect("post.jsp");
        }

        public IGreetingService getClient() {
            return client;
        }

        public void setClient(IGreetingService client) {
            this.client = client;
        }

}

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
    xmlns:jaxws="http://cxf.apache.org/jaxws">

     <!-- enable transaction demarcation with annotations -->
     <tx:annotation-driven />

    <!-- enable autowire -->
     <context:annotation-config  />

    <bean id="clientServlet" class="com.cxf.spring.service.ClientServlet">
      <property name="client" ref="client"></property>
      </bean>

    <bean id="client" class="com.cxf.spring.service.IGreetingService"
     factory-bean="clientFactory" factory-method="create" />
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.cxf.spring.service.IGreetingService" />
        <property name="address" value="http://localhost:8080/CxfSpring/greet" />
    </bean>

     <jaxws:client id="greetingService"
         serviceClass="com.cxf.spring.service.IGreetingService"
         address="http://localhost:8080/CxfSpring/greet">
    </jaxws:client>  

</beans>

<?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_3_0.xsd"
    version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
         <param-value>classpath:beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

     <servlet>
        <servlet-name>clientServlet</servlet-name>
        <servlet-class>com.cxf.spring.service.ServletToBeanProxy</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>clientServlet</servlet-name>
        <url-pattern>/testClient</url-pattern>
    </servlet-mapping>

      <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>

</web-app>

所需jar包:

时间: 2024-10-14 10:10:48

CXF集成Spring实现webservice的发布与请求的相关文章

一个CXF集成SPRING的WEBSERVICE完整实例

1 首先准备以下JAR包 activation.jar commons-logging-1.1.1.jar cxf-2.5.6.jar jaxb-api-2.2.1.jar jaxb-impl-2.1.3.jar jaxws-api-2.2.8.jar neethi-3.0.2.jar ow2-jws-2.0-spec-1.0.11.jar spring-aop-3.0.5.RELEASE.jar spring-asm-3.0.5.RELEASE.jar spring-beans-3.0.5.R

CXF整合Spring开发WebService

刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开环境变量配置窗口,点击新建,新建%CXF_HOME%变量,值为你下载的cxf所在的目录,我的是D:\tools\apache-cxf-3.1.0 2.在Path变量中新加一句%CXF_HOME%\lib,注意要和已有的path变量用;隔开 3.在CLASSPATH中新加一句%CXF_HOME%\li

cxf整合spring实现webservice

前面一篇文章中,webservice的服务端与客户端都是单独启动,但是在现实项目中,服务端单独启动太没有实际意义了,还是要整合框架启动,所以今天将记录如何整合spring框架. jar包下载地址如下: http://yun.baidu.com/share/link?shareid=547689626&uk=2836507213 (一).web.xml中添加spring与cxf的配置 <?xml version="1.0" encoding="UTF-8"

Cxf 集成spring完整示例

1 CXF概述 1.1 CXF介绍 Apache CXF =Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding .DataBinding.Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)

使用CXF和spring搭建webService服务

虽然下一个项目需要使用xfire,但是在查资料的过程中还是看到有不少地方都说cxf比xfire更好,cxf继承了xfire,但是不仅仅包含xfire,因此便也一起来尝试尝试.大概是有了xfire的经验吧,cxf的搭建比xfire快了许多. cxf的许多参数感觉和xfire差不多,因此便不做太多的解释,如果不明白的可以参考之前的xfire搭建来促进理解. 搭建过程如下: 1.使用eclipse创建一个maven项目,maven导包的pom.xml的整体内容如下: <project xmlns=&quo

xfire集成spring构建webservice

前言:xfire.spring都是比较流行的技术,这里就不再赘述他们各自的优点:本文着重介绍xfire和spring的整合,不会做太深入的探究. 服务端 1. web.xml配置 spring配置部分:contextConfigLocation定义配置文件路径,可以指定全局的配置文件路径. <!-- spring配置 --> <context-param> <param-name>contextConfigLocation</param-name> <

jfinal集成spring cxf做webservice服务

链接地址:http://zhengshuo.iteye.com/blog/2154047 废话不说,直接上代码 新增cxf的plugin CXFPlugin Java代码   package com.jfinal.plugin.spring; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBusFactory; import org.apache.cxf.transport.servlet.ServletTra

WebService—CXF整合Spring实现接口发布和调用过程2

一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.18</version> </dependency> <de

webservice的cxf和spring整合发布

1.新建一个web项目 2.导入cxf相应的jar包,并部署到项目中 3.服务接口 1 package com.xiaostudy; 2 3 /** 4 * @desc 服务器接口 5 * @author xiaostudy 6 * 7 */ 8 public interface Test_service { 9 10 public String getNumber(String number); 11 12 } 4.服务接口实现类 1 package com.xiaostudy; 2 3 im