天天看點

部署WebService服務(cxf,spring)

使用CXF,spring部署Web Service服務

[b]server 實作[/b]

步驟一 建立Web工程

步驟二 下載下傳cxf包(位址:http://www.apache.org/dyn/closer.cgi?path=/cxf/2.6.2/apache-cxf-2.6.2.tar.gz)

步驟三 解壓cxf包,将apache-cxf-*.*.*/lib目錄下的jar包複制到Web工程->WebRoot->WEB-INF->lib檔案夾下(cxf已經整合了spring是以不需要在導入spring的包了)。不必導入全部jar包 具體見附件圖檔

步驟四 在Web工程下建立一個接口并定義一個方法

例:

package com.cxf;

import javax.jws.WebParam;

import javax.jws.WebService;

@WebService

public interface ISayHello

{

public String sayHello(@WebParam(name="name")String name);

}

步驟五 建立一個類繼承接口

例:

package com.cxf;

import javax.jws.WebService;

@WebService(endpointInterface = "com.cxf.ISayHello")

public class SayHelloImpl implements ISayHello

{

@Override

public String sayHello(String name)

{

return "my name is " + name;

}

}

步驟六 在工程src目錄下建立spring配置檔案applicationContext.xml

applicationContext.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/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />

<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<bean id="sayHelloImpl" class="com.cxf.SayHelloImpl" />

<jaxws:endpoint id="sayHello" implementor="#sayHelloImpl" address="/sayHello" />

</beans>

步驟七 修改Web工程->WebRoot->WEB-INF->web.xml

在web.xml檔案<web-app></web-app>中加上:

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

classpath:applicationContext.xml

</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

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

釋出我們建立的Web工程

打開浏覽器通路:http://localhost:8080/WebService/

[b]

client 實作[/b]

步驟一 在工程中建立一個類(可以重建立一個工程但需要導入cxf的相關包)

例:

package com.cxf.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cxf.ISayHello;

public class CxfClient

{

public static void main(String[] args)

{

ClassPathXmlApplicationContext context

= new ClassPathXmlApplicationContext(new String[] {"com/cxf/client/applicationContext_client.xml"});

ISayHello client = (ISayHello)context.getBean("sayHello2");

String response = client.sayHello("Joe");

System.out.println("Response: " + response);

System.exit(0);

}

}

步驟二 在client同級目錄建立client spring配置檔案applicationContext_client.xml

applicationContext_client.xml檔案内容如下:

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:jaxws="http://cxf.apache.org/jaxws"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

<!--

該方式通路ws服務也是利用spring的依賴注入法

id是spring IOC容器唯一辨別符

serviceClass是webservices服務接口

address是服務wsdl位址

-->

<jaxws:client id="sayHello2" serviceClass="com.cxf.ISayHello" address="http://localhost:8080/WebService/sayHello?wsdl" />

</beans>

執行CxfClient的main方法

控制台列印

Response: my name is Joe

在完成執行個體中所遇問題

啟動tomcat抛:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sayHello': Invocation of init method failed; nested exception is java.lang.LinkageError: JAXB 2.1 API is being loaded from the bootstrap classloader, but this RI (from jar:file:/D:/apache-tomcat-6.0.29/webapps/WebService/WEB-INF/lib/jaxb-impl-2.2.4-1.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.2 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.6.0/docs/guide/standards/)

Caused by: java.lang.LinkageError: JAXB 2.1 API is being loaded from the bootstrap classloader, but this RI (from jar:file:/D:/apache-tomcat-6.0.29/webapps/WebServiceCXF/WEB-INF/lib/jaxb-impl-2.2.4-1.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.2 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.6.0/docs/guide/standards/)

問題原因:cxf與jdk jar包沖突

解決方法:

1.更新JDK到jdk1.6.0_25(據說jdk1.6.0_25後就支援了),然後修改tomcat的執行jdk版本(還有另一種方法,可以google下)

問題二:

執行客服端類main方法時抛:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [demo/spring/client/client-beans.xml]; nested exception is javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found

問題原因:

是我使用MyEclipse建立項目時引用了J2EE 1.4 Library Container庫

解決方法:

1.删掉J2EE 1.4 Library 。

2.換為JAVA EE 5/6 Library