软件准备

Eclipse 4.7.2
Maven 3.5.3
Spring 3.9.2
CXF 3.2.6

Eclipse的下载可以去spring官网

其他软件的下载和使用,请自行问度娘。

步骤

1. 新建web工程,利用Maven管理,如下:

image

image

工程名为springCXFWebService,完成以后,项目结构如下图:

image

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

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

src/test/java 准备放测试相关的java程序。

2. 添加代码

1) 定义服务接口

1
2
3
4
5
6
7
8
9
10
11
12
package com.locqi.test;

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

@WebService
public interface HelloWorld {

@WebMethod
String sayHello();

}

因为只是一个WebService的实验程序,所以非常简单,只有一个服务方法: sayHello(),利用 @WebService注解来声明这是一个WebService的接口。

2) 实现服务类

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.locqi.test.impl;

import javax.jws.WebService;

import com.locqi.test.HelloWorld;

@WebService
public class HelloWorldImpl implements HelloWorld {

public String sayHello() {
return "say Hello World!";
}
}

完成java代码添加后的项目结构如下:

image

3. 添加Spring-CXF 配置

在项目 src/main/webapp/WEB-INF 目录下新建XML定义:cxf-servlet.xml如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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.locqi.test.impl.HelloWorldImpl" address="/HelloWorld"/>
</beans>
<!-- END SNIPPET: beans -->

该定义文件利用spring和CXF的功能,发布一个ID为helloWorld,实现类为com.locqi.test.impl.HelloWorldImpl,发布相对路径为 /HelloWorld(对应绝对目录为:_http://host:port/{WebAPPName}/HelloWorld_)的 WebService。

因为我们需要用到CXF来做WebService,右键点击项目中的pom.xml,添加apache-cxf依赖,如下图:
image

当然,也可以在pom.xml文件中添加以下apache-cxf依赖的配置信息:

1
2
3
4
5
6
<dependency>			
<groupId>org.apache.cxf</groupId>
<artifactId>apache-cxf</artifactId>
<version>3.2.6</version>
<type>pom</type>
</dependency>

然后,Update Maven Project,如下图:

image

注意!更新后的java build path需要重新指定哦!

4. Web应用配置

修改 src/main/webapp/WEB-INF 目录下的web.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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以后的工程目录如下:

image

5. 编译打包

利用maven(package -X)编译打包成springCXFWebService.war
(在Eclipse上右击工程名 Run as -> Maven build)

6. 将步骤5生成的springCXFWebService.war部署到tomcat服务器上

7. 访问测试:

在浏览器上输入:http://localhost:8080/springCXFWebService/,出现如下画面就成功了:

image

点击WSDL链接:

image

8. 编写WebService client端代码

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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.locqi.testClient.HelloWorldClient"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.locqi.test.HelloWorld"/>
<property name="address" value="http://localhost:8080/springCXFWebService/HelloWorld"/>
</bean>
</beans>
<!-- END SNIPPET: beans -->

需要注意的是,该配置文件中的 address需要写成发布服务的绝对路径。

2) 编写客户端java代码: HelloWorldClient.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.locqi.testClient;

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

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

现在的项目结构如下:

image

3) 连接测试

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

image

输出的say Hello World!! 即是我们发布的HelloWorld的方法 sayHello()的输出!这说明从服务发布到客户端连接都成功了。

DEMO下载地址

最后更新: 2018年09月06日 18:12

× 请我吃糖~
打赏二维码