您的位置:首页 > 运维架构 > Apache

Apache CXF简单WebService例子 .

2013-06-25 16:51 309 查看
一.开发环境

我的开发环境是:

MyEclipse 6.5

Tomcat 6.0

Apache cxf-2.3.0 下载地址: http://www.apache.org/dist/cxf/2.3.0/

二.开发步骤

创建Server

1. 将下载的cxf-2.3.0 解压。然后在MyEclipse里面添加一个

User Library名字叫library-cxf-2.3.0 把 lib下的包都添加进去。

2.新建一个Web工程cxfdemo,并添加library-cxf-2.3.0,然后建个com.demo包。在包里面创建HelloWorld接口

代码如下

[java]
view plaincopyprint?

/**
* HelloWorld.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉 Dec 2, 2010 9:47:44 AM
*/
package com.demo;

import javax.jws.WebService;

/**
* @author 崔冉
* @version 1.0.0
* @desc 接口HelloWorld
*/
@WebService
public interface HelloWorld {
String sayHi(String text);
}

[java]
view plaincopyprint?

/**
* HelloWorldImpl.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉  Dec 2, 2010 9:49:17 AM
*/
package com.demo;

import javax.jws.WebService;

/**
* @author 崔冉
* @version 1.0.0
* @desc 实现HelloWorld接口
*/
@WebService
public class HelloWorldImpl implements HelloWorld {

/**
*
*/
public String sayHi(String text) {

return "Hi,"+text;
}

}

/**
* HelloWorldImpl.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉  Dec 2, 2010 9:49:17 AM
*/
package com.demo;

import javax.jws.WebService;

/**
* @author 崔冉
* @version 1.0.0
* @desc 实现HelloWorld接口
*/
@WebService
public class HelloWorldImpl implements HelloWorld {

/**
*
*/
public String sayHi(String text) {

return "Hi,"+text;
}

}


然后修改web.xml里面内容

[xhtml]
view plaincopyprint?

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>

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

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-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-app>

[xhtml]
view plaincopyprint?

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

<jaxws:endpoint id="helloWorld" implementor="com.demo.HelloWorldImpl" address="/HelloWorld" />
</beans>

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

<jaxws:endpoint id="helloWorld" implementor="com.demo.HelloWorldImpl" address="/HelloWorld" />
</beans>


将Web工程发布到Tomcat里面,然后启动Server

创建Client

接着创建客户端,在com.demo下面创建client-beans.xml。内容如下

[xhtml]
view plaincopyprint?

<?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.HelloWorld"
factory-bean="clientFactory" factory-method="create" />

<bean id="clientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.demo.HelloWorld" />
<property name="address" value="http://127.0.0.1:8080/cxfdemo/HelloWorld" />
</bean>

</beans>

[java]
view plaincopyprint?

/**
* Client.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉  Dec 2, 2010 9:53:30 AM
*/
package com.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author 崔冉
* @version 1.0.0
* @desc 测试类
*/
public class Client {
public static Client self = new Client();
private HelloWorld client = null;

private Client() {
// START SNIPPET: client

ApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"com/demo/client-beans.xml"});
client = (HelloWorld) context.getBean("client");
}

public HelloWorld getServer() {

return client;

}

public String getText(String text) throws Exception {
String response = getServer().sayHi(text);
return response;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try {

System.out.println(self.getText("cuiran"));
} catch (Exception e) {
// TODO Auto-generated catch block

e.printStackTrace();
}

}

}

/**
* Client.java
* 版权所有(C) 2010 cuiran2001@163.com
* 创建:崔冉  Dec 2, 2010 9:53:30 AM
*/
package com.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author 崔冉
* @version 1.0.0
* @desc 测试类
*/
public class Client {
public static Client self = new Client();
private HelloWorld client = null;

private Client() {
// START SNIPPET: client
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"com/demo/client-beans.xml"});
client = (HelloWorld) context.getBean("client");
}

public HelloWorld getServer() {

return client;

}

public String getText(String text) throws Exception {
String response = getServer().sayHi(text);
return response;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {

System.out.println(self.getText("cuiran"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


然后进行测试运行。如果遇到类似这种错误“AXB 2.1 API is being loaded from the bootstrap classloader”说明你的web工程添加了J2EE 1.4或者是1.5 library. 这样就与jaxb-api-2.2.1.jar

冲突了,只需删除即可。

最后运行如图



源码下载地址:http://dl.dbank.com/c0vhvbrxlr

对于jar包下载 http://dl.dbank.com/c0y67j2hqq
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: