1、準備java
軟件環境:web
JDK1.8, Eclipse JEE 4.4, Maven-3.2.5, Spring-4, CXF-3.1.5spring
2、建立項目express
<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>
<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]apache
3、編寫類文件api
@WebService public interface ICXFService{ @WebMethod public String sayHello(@WebParam(name="username") String username); }
說明: @WebService:標記表示該接口是一個WebService服務。 @WebMethod:標記表示WebService中的方法。 @WebParam(name="paramName")表示方法中的參數,name屬性限制了參數的名稱,若沒有指定該屬性,參數將會被重命名。session
public class CXFService implements ICXFService{ public String sayHello(String username){ return "Hello, "+username; } }
<?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 -->
<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>
<?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); } }
4、遇到的問題app
Eclipse在建立Maven的時候會建立index.jsp文件。而pom.xml文件裏只有junit依賴,全部還需手動添加servlet-apiless
<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 newerjsp
若是遇到了,在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。