用cxf编写webservice
cxf的官网文档地址:http://cxf.apache.org/docs/
1.建立一个web project,名称为cfx-new,部署目录写成ROOT
2.下载最新的cxf包http://cxf.apache.org/download.html,我下载的是apache-cxf-2.5.2.zip,直接将lib下的所有jar拷贝到lib下
3.新建一个接口类HelloWorld
package info.frady.service;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface HelloWorld { String sayHi(String text);}
package info.frady.service.impl;import info.frady.service.HelloWorld;import javax.jws.WebService;@WebService(endpointInterface = "info.frady.service.HelloWorld", serviceName = "helloWorld")public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { return "Hello " + text; }}
<?xml version="1.0" encoding="UTF-8"?><!-- START SNIPPET: beans --><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.xsdhttp://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="info.frady.service.impl.HelloWorldImpl" address="/helloWorld" /></beans>
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><!-- START SNIPPET: webxml --><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>/services/*</url-pattern></servlet-mapping></web-app><!-- END SNIPPET: webxml -->
import info.frady.domain.Person;import info.frady.service.HelloWorld;import org.apache.cxf.interceptor.LoggingInInterceptor;import org.apache.cxf.interceptor.LoggingOutInterceptor;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class TestHello {/** * @param args */public static void main(String[] args) {JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.getInInterceptors().add(new LoggingInInterceptor());factory.getOutInterceptors().add(new LoggingOutInterceptor());factory.setServiceClass(HelloWorld.class);factory.setAddress("http://localhost:8080/services/HelloWorld");HelloWorld client = (HelloWorld) factory.create();String reply = client.sayHi("frady");System.out.println("Server said: " + reply);System.exit(0); }}