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

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

  1. CXF集成spring做webservice接口

    一 . cxf 的jar包 1.cxf-2.3.3.jar 2.wsdl4j-1.6.2.jar 3.wss4j-1.5.11.jar 4.wstx-asl-3.2.0.jar 5.XmlSchema ...

  2. CXF整合Spring开发WebService

    刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...

  3. CXF之四 cxf集成Spring

    CXF原生支持spring,可以和Spring无缝集成.WebService框架CXF实战一在Tomcat中发布WebService(二)通过Spring Web实现CXFServlet.下面将Spr ...

  4. 使用CXF和spring搭建webService服务

    虽然下一个项目需要使用xfire,但是在查资料的过程中还是看到有不少地方都说cxf比xfire更好,cxf继承了xfire,但是不仅仅包含xfire,因此便也一起来尝试尝试.大概是有了xfire的经验 ...

  5. xfire集成spring构建webservice

    前言:xfire.spring都是比较流行的技术,这里就不再赘述他们各自的优点:本文着重介绍xfire和spring的整合,不会做太深入的探究. 服务端 1. web.xml配置 spring配置部分 ...

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

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

  7. Dubbo集成Spring与Zookeeper实例

    >>Dubbo最佳实践 使用Dubbo结合Zookeeper和Spring,是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和 ...

  8. spring集成环境下的axis webservice的发布,调试

    在spring集成的环境下,无论你是ssh集成,还是ssi集成的情况下,发布webservice往往在调用的时候会出错. 特别是,如果你是这个方式: 将webservice打aar包,放到tomcat ...

  9. CXF整合Spring发布WebService实例

    一.说明: 上一篇简单介绍了CXF以及如何使用CXF来发布一个简单的WebService服务,并且介绍了客户端的调用. 这一篇介绍如何使用CXF与spring在Web项目中来发布WebService服 ...

随机推荐

  1. Noise Contrastive Estimation

    Notes from Notes on Noise Contrastive Estimation and Negative Sampling one sample: \[x_i \to [y_i^0, ...

  2. 点击.box跟点击.box.box1

    <!DOCTYPE html> <html> <head> <meta charset='UTF-8'> <title>click</ ...

  3. python学习笔记-(十三)堡垒机

    1.课前准备: 本次学习堡垒机相关知识:之前,需要安装Python的paramiko模块,该模块基于SSH用于连接远程服务器并执行相关操作. 前提: python3.5程序安装到默认路径下并已添加pa ...

  4. nodejs + ionic2 + cordova环境搭建

    1.nodejs 下载地址:https://nodejs.org/zh-cn/,执行默认安装就可以,可执行命令 node(环境变量) 2.修改 npm 源,执行:npm config set regi ...

  5. 在Azure上搭建Orchard CRM入口网站

    这是英文版:Setup Orchard CRM portal website on Azure

  6. ubuntu安装ssh

    为了解决远程连接ubuntu服务器控制端,方便操作.ubuntu不同的版本安装方式一致!首先在ubuntu服务器下安装SSH服务linux安装命令:sudo apt-get install opens ...

  7. .NET Core添加项目之间的依赖关系

  8. ie、火狐、谷歌浏览器,光标定位问题

    IE:不管该行有没有文字,光标高度与font-size一致. FF:该行有文字时,光标高度与font-size一致.该行无文字时,光标高度与input的height一致. Chrome:该行无文字时, ...

  9. C语言中char* 和 char []区别

    想要把丢掉的东西捡起来,还是很辛苦啊,今天我就发现,我连char* 和 char []的区别都不知道. 很多人觉得这两个定义效果一样,其实差别很大.以下是个人的一些看法,有不正确的地方望指正. 本质上 ...

  10. Redis安装教程

    1. Linux下Redis安装教程 (1)安装 #tar xf redis-2.6.14.tar.gz #cd redis-2.6.14 #make #make install (2)配置 修改re ...