1. 首先下載apache cxf壓縮包,到apache官方網站下載。將lib目錄下的jar文件都拷貝到本身的項目lib下面。這裏個人項目名爲cxfservicejava
2. 配置sring的配置文件以下:web
建立webservice服務接口spring
package com.cxf.test;apache
import javax.jws.WebService;api
@WebServicetomcat
public interface HelloWorld {測試
public String sayHello(String text);網站
}spa
package com.cxf.test;orm
import javax.jws.WebService;
實現webservice接口,指明服務所實現的接口
@WebService(endpointInterface="com.cxf.test.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
public String sayHello(String text) {
return "hi"+text+"您好";
}
}
3. 配置spring配置文件,配置以下
<?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">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="hello" class="com.cxf.test.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello"
address="/HelloWorld" />
</beans>
4. 到目前爲止咱們能夠啓動咱們的服務了。而後訪問咱們的服務http://localhost:8080/cxfservice/HelloWorld?wsdl,會發現報錯,這是由於tomcat自帶的jar文件和cxf的jar文件衝突。解決方法。在tomcat跟目錄下新建文件common\endorsed。將jaxb-api-2.2.1.jar和jaxb-impl-2.2.1.1.jar兩個jar文件拷貝到裏面去。ok而後啓動服務,從新訪問一下就會看到生成的wsdl文件了。這時候咱們的holleword的webservice服務端就已經建立完了。
5. 如何經過webservice客戶端訪問咱們發佈的webservice服務呢。首先建立一個java項目,就叫cxclient,建立接口
package com.cxf.test;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String text);
}
6. Spring的配置以下所示:
<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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="com.cxf.test.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.cxf.test.HelloWorld"/>
<property name="address" value="http://localhost:8080/cxfservice/HelloWorld"/>
<!-- 這個地方的地址必定要注意,正確的-->
</bean>
</beans>
7. 好了。最後讓咱們來測試一下吧。上測試程序。
package com.cxf.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
HelloWorld client = (HelloWorld) ctx.getBean("client");
String result = client.sayHello("你好!");
System.out.println(result);
}
}