Eclipse+Maven+Spring+CXF 构建webservice 服务

Posted blfbuaa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Eclipse+Maven+Spring+CXF 构建webservice 服务相关的知识,希望对你有一定的参考价值。

 

一、   软件准备

Eclipse 4.2.1

Maven 2.2.1

Spring 3.2.6

CXF 3.0.2

软件下载和Eclipse 安装 maven插件等请參考其它文章。

二、   步骤

1.        新建webproject,利用maven管理。例如以下:

 技术分享 

技术分享

project名为test,完毕以后。项目结构例如以下图:

技术分享

src/main/java 准备放 java 程序。

src/main/resources准备放各类资源文件。

2.        加入代码

1)        定义服务接口

package com.test;
 
import javax.jws.WebService;
 
@WebService
public interface HelloWorld {
    public String sayHello();
}


由于仅仅是一个webservice的实验程序,所以很easy。仅仅有一个服务方法: sayHello(),利用 @WebService注解来声明这是一个webservice的接口。

 

2)        实现服务类

package com.test;
 
import javax.jws.WebService;
 
@WebService
public class HelloWorldImpl implements HelloWorld{
   
    public String sayHello(){
        return "Hello world!";
    }
}

完毕java代码加入后的项目结构例如以下:

技术分享

 

3.        加入Spring-CXF 配置

在项目 src/main/webapp/WEB-INF 文件夹下新建XML定义:cxf-servlet.xml例如以下:

<?

xml version="1.0" encoding="UTF-8"?

> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- START SNIPPET: beans --> <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"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:endpoint id="helloWorld" implementor="com.test.HelloWorldImpl" address="/HelloWorld"/> </beans> <!-- END SNIPPET: beans -->

该定义文件利用spring和CXF的功能,公布一个ID为helloWorld,实现类为com.test.HelloWorldImpl,公布相对路径为 /HelloWorld(相应绝对文件夹为: http://host:port/{WebAPPName}/HelloWorld)的 webservice。

 由于我们须要用到CXF来做webservice。右键点击项目中的POM.XML。加入apache-cxf依赖。例如以下图:

技术分享

 

4.        Web应用配置

改动 src/main/webapp/WEB-INF 文件夹下的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements. See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership. The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License. You may obtain a copy of the License at
 
  http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied. See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 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>
    <servlet>
        <description>Apache CXF Endpoint</description>
        <display-name>cxf</display-name>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>

该文件实际上是定义了处理webservice的CXF Servlet的映射关系。

 完毕步骤3和4以后的project文件夹例如以下:

技术分享

5.        编译打包

利用maven(package -X)编译打包成test.war

(在Eclipse上右击project名 Run as -> Maven build)

6.        将步骤5生成的test.war部署到tomcatserver上

7.        訪问測试:

在浏览器上输入:http://localhost:8080/test/。出现例如以下画面就成功了:

技术分享

点击WSDL链接:

技术分享 

8.        编写webservice client端代码

1)        首先通过 Spring 与 CXF 的配置来定义 Web Service 的客户端 Bean,在 src\main\resources 文件夹下创建client-beans.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--
        Licensed to the Apache Software Foundation (ASF) under one
        or more contributor license agreements. See the NOTICE file
        distributed with this work for additional information
        regarding copyright ownership. The ASF licenses this file
        to you under the Apache License, Version 2.0 (the
        "License"); you may not use this file except in compliance
        with the License. You may obtain a copy of the License at
       
        http://www.apache.org/licenses/LICENSE-2.0
       
        Unless required by applicable law or agreed to in writing,
        software distributed under the License is distributed on an
        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
        KIND, either express or implied. See the License for the
        specific language governing permissions and limitations
        under the License.
-->
<!-- START SNIPPET: beans -->
<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/schema/jaxws.xsd">
    <bean id="client" class="com.test.HelloWorld"
        factory-bean="clientFactory" factory-method="create"/>
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.test.HelloWorld"/>
        <property name="address" value="http://localhost:8080/test/HelloWorld"/>
    </bean>
</beans>
<!-- END SNIPPET: beans -->

须要注意的是。该配置文件里的 address须要写成公布服务的绝对路径。

 

2)        编写clientjava代码: HelloWorldClient.java

package com.test;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public final class HelloWorldClient {
 
    private HelloWorldClient() {
    }
 
    public static void main(String args[]) throws Exception {
        // START SNIPPET: client
        ClassPathXmlApplicationContext context
            = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
 
        HelloWorld client = (HelloWorld)context.getBean("client");
 
        String response = client.sayHello();
        System.out.println("Response: " + response);
        System.exit(0);
        // END SNIPPET: client
    }
}

注意。代码中HelloWorldclient = (HelloWorld)context.getBean("client"); 的client须要与"client-beans.xml"中的 bean id一致才干找到这个服务。 

如今的项目结构例如以下:

技术分享

3)        连接測试

在eclipse中直接按HelloWorldClient执行 Run as -> Java Application: 

技术分享

 输出的Hello world! 即是我们公布的HelloWorld的方法 sayHello()的输出!这说明从服务公布到client连接都成功了。

 











以上是关于Eclipse+Maven+Spring+CXF 构建webservice 服务的主要内容,如果未能解决你的问题,请参考以下文章

Spring+CXF+Maven发布Webservice

CXF+Spring+Eclipse简明示例

cxf3.1+spring4+maven启动报错

maven版cxf集合spring开发服务端

系统开发系列 之WebService(spring框架+maven+CXF框架)部署问题及解决方案

系统开发系列 之WebService(spring框架+maven+CXF框架)部署问题及解决方案