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

使用Apache的CXF框架发布Webservice四种方法总结

2013-06-17 14:07 495 查看
注意:jar包还是*.jar哦!!!

第一种:用一个j2se的main方法来发布

[java] view
plaincopyprint?

public class Server {

public static void main(String[] args) {

Endpoint.publish("http://127.0.0.1:8080/cxf", new HelloImpl());

}

}

第二种用tomcat来发布

webX.xml:

[xhtml] view
plaincopyprint?

<servlet>

<servlet-name>CXFServlet</servlet-name>

<servlet-class>t.T</servlet-class>

<init-param>

<param-name>/hello</param-name>

<param-value>t.HelloImpl</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>CXFServlet</servlet-name>

<url-pattern>/services/*</url-pattern>

</servlet-mapping>

t.java:

[java] view
plaincopyprint?

package t;

import java.util.Enumeration;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;

import org.apache.cxf.BusFactory;

import org.apache.cxf.transport.servlet.CXFNonSpringServlet;

public class T extends CXFNonSpringServlet {

private static final long serialVersionUID = -4143021604478775522L;

public void loadBus(ServletConfig servletConfig) throws ServletException {

super.loadBus(servletConfig);

Bus bus = this.getBus();

BusFactory.setDefaultBus(bus);

// 获取在web.xml中配置的要发布的所有的Web服务实现类并发布Web服务

Enumeration<String> enumeration = this.getInitParameterNames();

while (enumeration.hasMoreElements()) {

String key = enumeration.nextElement();

String value = this.getInitParameter(key);

try {

Class clazz = Class.forName(value);

try {

Endpoint.publish(key, clazz.newInstance());

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

}

第三种:还是用tomcat,但是采用spring的一些东东。

web.xml

[xhtml] view
plaincopyprint?

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

<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>/services/*</url-pattern>

</servlet-mapping>

同目录还有一个beans.cml:

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

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

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 http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.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-extension-jaxrs-binding.xml" />

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

<jaxws:endpoint id="helloServiceWs" address="/hello" implementor="#helloService" />

<bean id="helloService" class="t.HelloImpl" />

</beans>

再说一哈,客户端代码生成总结!!!这里提一下而已,多个思路。

1.手写:

[java] view
plaincopyprint?

public class Client {

public static void main(String[] args) {

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

factory.setAddress("http://127.0.0.1/cxf_server/services/hello");

factory.setServiceClass(HelloInter.class);

HelloInter helloInter = (HelloInter) factory.create();

helloInter.hello();

}

}

2.用eclipse自动生成!

3.用XFire里面有的东西也可以生成。

4.用spring也可以生成。

5.用图形界面工具,比如soapui。(最简单)

6.用ant自动生成等等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: