天天看点

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