webservice整合spring cxf

下载cxf包,把他里面的包都添加进lib文件夹中。

创建一个接口。添加@WebService注解

@WebService
public interface HelloWorld {

    String sayHi(@WebParam(name="text")String text);
    String sayHiToUser(User user);
    String[] SayHiToUserList(List<User> userList);
}

创建接口的实现类,也添加@WebService注解,给他一个名称:boy

@WebService(serviceName="boy")
public class HelloWorldImpl implements HelloWorld {

    Map<Integer, User> users = new LinkedHashMap<Integer, User>();

    public String sayHi(String text) {
        return "Hello " + text;
    }

    public String sayHiToUser(User user) {
        users.put(users.size() + 1, user);
        return "Hello " + user.getName();
    }

    public String[] SayHiToUserList(List<User> userList) {
        String[] result = new String[userList.size()];
        int i = 0;
        for (User u : userList) {
            result[i] = "Hello " + u.getName();
            i++;
        }
        return result;
    }

}

User实体类:

public class User {

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

}

服务端代码:

public class webServiceApp {
      public static void main(String[] args) {
          System.out.println("web service start");
          HelloWorldImpl implementor= new HelloWorldImpl();
          String address="http://localhost:8000/helloWorld";
          Endpoint.publish(address, implementor);
          System.out.println("web service started");
 }
}

客户端代码:有集成spring和没集成的

public class HelloWorldClient {
    public static void main(String[] args) {

        //集成spring和cxf
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld client1 = (HelloWorld) context.getBean("client1");
        User user1 = new User();
        user1.setName("Tony");
        user1.setAge(20);
        User user2 = new User();
        user2.setName("freeman");
        user2.setAge(50);
        List<User> userList = new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);
        // String[] res = client1.SayHiToUserList(userList);
        // System.out.println(res[0]);
        // System.out.println(res[1]);
        String sayHi = client1.sayHi("good");
        System.out.println(sayHi);

        // HelloWorld2 client2 = (HelloWorld2) context.getBean("client2");
        // User user3 = new User();
        // user3.setName("Jerry");
        // user3.setAge(20);
        // String user = client2.sayHiToUser(user3);
        // System.out.println(user);

        //没有集成
        JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
        svr.setServiceClass(HelloWorld.class);
        svr.setAddress("http://localhost:8000/helloWorld");
        HelloWorld hw = (HelloWorld) svr.create();
        User user = new User();
        user.setName("Tony");
        System.out.println(hw.sayHi("ffff"));

    }
}

打印的结果是:

Hello good

Hello ffff

web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name>cxf</display-name>
 <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

                <context-param>
                          <param-name>contextConfigLocation</param-name>
                          <param-value>classpath*:applicationContext.xml</param-value>
                 </context-param>

              <listener>
                      <listener-class>
                              org.springframework.web.context.ContextLoaderListener
                      </listener-class>
              </listener>

                <listener>
                    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
                </listener>

              <servlet>
                     <servlet-name>CXFServlet</servlet-name>
                     <servlet-class>
                            org.apache.cxf.transport.servlet.CXFServlet
                     </servlet-class>
                     <load-on-startup>1</load-on-startup>
               </servlet>

               <servlet-mapping>
                      <servlet-name>CXFServlet</servlet-name>
                      <url-pattern>/*</url-pattern>
               </servlet-mapping>
</web-app>

applicationContext.xml的配置,这个文件是放在资源文件resources下的,他在项目中的路径是在classes里面的。我在这里配置了两个站点。都是可以的。

<?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: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">

    <!-- 发布一个站点 -->
    <jaxws:endpoint id="boy" implementor="com.cs.webservice.HelloWorldImpl"
        address="/aaa" />

    <bean id="client1" class="com.cs.webservice.HelloWorld"
        factory-bean="clientFactory1" factory-method="create" />

    <bean id="clientFactory1" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.cs.webservice.HelloWorld" />
        <property name="address" value="http://localhost:8000/helloWorld/aaa" />
    </bean>

    <!-- 第二个站点 -->
<!--     <jaxws:endpoint id="orange" implementor="com.cs.webservice.HelloWorldImpl2"
        address="/bbb" />

    <bean id="client2" class="com.cs.webservice.HelloWorld2"
        factory-bean="clientFactory2" factory-method="create" />

    <bean id="clientFactory2" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.cs.webservice.HelloWorld2" />
        <property name="address" value="http://localhost:8080/cxfwebservicespring/bbb" />
    </bean> -->
</beans>
<property name="address" value="http://localhost:8000/helloWorld/aaa" />aaa前面的路径必须与发布的String address="http://localhost:8000/helloWorld";路径相同。
时间: 2024-10-07 21:54:33

webservice整合spring cxf的相关文章

CXF WebService整合Spring

转自http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html 首先,CXF和spring整合需要准备如下jar包文件: 这边我是用Spring的jar包是Spring官方提供的,并没有使用CXF中的Spring的jar文件. 添加这么多文件后,首先在web.xml中添加如下配置: <!-- 加载Spring容器配置 --> <listener> <listener-class>org.springframe

Webservice整合Spring进行校验

服务端代码: 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/xm

webservice整合spring

接口HelloWorld需要添加webservice注解 package com.cs.webservice; import java.util.List; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(@WebParam(name="text")String text); String sayHiToUser(

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"

httpclient4.x调用cxf发布的webservice的某个方法(有参数,有返回值)(未整合spring)

原文:httpclient4.x调用cxf发布的webservice的某个方法(有参数,有返回值)(未整合spring) 源代码下载地址:httpclient4.x调用cxf发布的webservice的某个方法(有参数,有返回值)(未整合spring) 采用spring + cxf编写服务端 httpclient4编写客户端调用 如题,代码没有jar 完整包: 链接:http://pan.baidu.com/share/link?shareid=2162612373&uk=402880896 密

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

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实现接口发布和调用过程

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

【Java EE 学习第81天】【CXF框架】【CXF整合Spring】

一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一套开发WebService服务的标准.早期的标准如jax-rpc已经很少使用,而cxf就是在新标准jax-ws下开发出来的WebService,jax-ws也内置到了jdk1.6当中. CXF官方下载地址:http://cxf.apache.org/download.html 下载完成之后,解压开压