一、准备
软件环境:
JDK1.8, Eclipse JEE 4.4, Maven-3.2.5, Spring-4, CXF-3.1.5
二、创建项目
- 新建一个Maven项目,在pom.xml里添加spring依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>1.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 添加cxf依赖,或是从apache cxf官网下载已编译的文件,通过BuildPath添加
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>apache-cxf</artifactId>
<version>3.1.5</version>
<type>zip</type> //因为Maven远程仓库只有zip,tar.gz等压缩包,没有对应jar包,所有要指定type
</dependency>
用过BuildPath添加 右键项目---》 Build Path---》 Configure Build Path---》 Add Library---》 在对话框中选择CXF Runtime---》 如果是第一自使用CXF,则需要配置CXF,点击Configure installed runtimes,新增一个CXF---》 添加成功,在Libraries里会多出一个Apache CXF Libary[3.1.5]
三、编写类文件
- 新建一个接口类
@WebService
public interface ICXFService{
@WebMethod
public String sayHello(@WebParam(name="username") String username);
}
说明: @WebService:标记表示该接口是一个WebService服务。 @WebMethod:标记表示WebService中的方法。 @WebParam(name="paramName")表示方法中的参数,name属性限制了参数的名称,若没有指定该属性,参数将会被重命名。
- 新建一个实现类
public class CXFService implements ICXFService{
public String sayHello(String username){
return "Hello, "+username;
}
}
- 在WEB-INF下新建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" />
<jaxws:endpoint id="sayHello" implementor="com.demo.cxfws.service.impl.CXFServiceImpl" address="/sayHello" />
</beans>
<!-- END SNIPPET: beans -->
- 在web.xml中添加cxf配置信息
<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>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
- 在src\main\resources下新建client-beans.xml
<?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/schema/jaxws.xsd">
<bean id="client" class="com.demo.cxfws.service.ICXFService" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.demo.cxfws.service.ICXFService" />
<property name="address" value="http://localhost:8080/cxfws/sayHello" />
</bean>
</beans>
- 编写测试
public class TestCXF {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
ICXFService service = (ICXFService) context.getBean("client");
String str = service.sayHello("zhangsan");
System.out.println(str);
}
}
四、遇到的问题
Eclipse在创建Maven的时候会创建index.jsp文件。而pom.xml文件里只有junit依赖,所有还需手动添加servlet-api
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
还有一个问题如下Eclipse JAX-RS (REST Web Services) 2.0 requires Java 1.6 or newer
如果遇到了,在pom.xml添加:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
如果还不行,右键项目,点击Properties,在Java Compiler里选择Compiler Level为1.8
项目报错提示没有src\main\java,src\test\resources,src\test\java等文件夹
解决办法:进入Java Build Path下的Source选卡,移除打小红叉的文件夹,然后自己右键项目新建Source Folder。
来源:oschina
链接:https://my.oschina.net/u/2423277/blog/649150