使用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自动生成等等。
第一种:用一个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自动生成等等。
相关文章推荐
- 使用Apache的CXF框架开发WebService实例
- 使用CXF框架发布webservice
- 使用Cxf发布Webservice服务,如果待发布的接口中有重载方法,怎么处理??[转]
- webservice(三) 使用JDK的EndPoint和cxf框架分别发布webservice服务
- 使用CXF框架发布SOAP格式的webservice
- webService总结(三)——使用CXF + Spring发布webService
- 使用cxf发布webservice总结
- webService总结(一)——使用CXF发布和调用webService(不使用Spring)
- webService总结(二)——使用CXF手动发布webService(不使用Spring)
- 使用Apache的CXF框架开发WebService实例
- 使用Cxf发布Webservice服务,如果待发布的接口中有重载方法,怎么处理??
- Spring 集成CXF框架发布Webservice服务 和 使用jdk生成Webservice clinet
- 使用CXF框架发布WebService
- 使用CXF框架,发布webservice服务,并使用客户端远程访问webservice
- webservices——发布CXF的方法总结!!!
- 使用Apache发布delphi开发的isapi类型的webservice
- 四种apache禁止使用ip访问的实现方法
- 真正的轻量级WebService框架——使用JAX-WS(JWS)发布WebService
- grails框架中webService插件的使用(axis2,cxf)
- 使用Jetty服务器和Axis2框架技术发布Webservice接口