CXF 2.3 集成Spring3.0入门 HelloWorld


最少依赖包:

CXF 2.3 集成Spring3.0入门 HelloWorld

web.xml 配置

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<display-name>cxf-ws</display-name>
	<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>/ws/*</url-pattern>  
	</servlet-mapping>
</web-app>

 

cxf 配置  bean.xml

 

 

<?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.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.zx.cxf.service.HelloWorldImpl"    
      address="/HelloWorld" />
         
</beans>  
<!-- END SNIPPET: beans -->  

java 程序源代码 :

 

接口HelloWorld

 

 

package com.zx.cxf.service;

import java.util.Date;
import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
	String sayHi(@WebParam(name = "text") String text);

	Date curDate();

	List<Long> addNumber(@WebParam(name = "addToNum") Long num);
}

web service 实现类 HelloWorldImpl

 

 

 

package com.zx.cxf.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(endpointInterface = "com.zx.cxf.service.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
	private List<Long> localNumbers;
	
	@WebMethod(operationName="sayHello",action="hello")
	public String sayHi(@WebParam(name = "text") String text) {
		return "Hello " + text;
	}
	@WebMethod(exclude=true)
	public Date curDate() {
		return new Date();
	}

	public List<Long> addNumber(@WebParam(name = "addToNum") Long num) {
		if (localNumbers == null) {
			localNumbers = new ArrayList<Long>();
		}
		localNumbers.add(num);
		return localNumbers;
	}
}

启动,访问路径 http://localhost:8080/cxf-ws/ws/HelloWorld?wsdl 看到以下页面就表示启动成功

 

 

<wsdl:definitions name="HelloWorld" targetNamespace="http://service.cxf.zx.com/"><wsdl:types><xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://service.cxf.zx.com/"><xsd:element name="sayHi" type="tns:sayHi"/><xsd:complexType name="sayHi"><xsd:sequence><xsd:element minOccurs="0" name="text" type="xsd:string"/></xsd:sequence></xsd:complexType><xsd:element name="sayHiResponse" type="tns:sayHiResponse"/><xsd:complexType name="sayHiResponse"><xsd:sequence><xsd:element minOccurs="0" name="return" type="xsd:string"/></xsd:sequence></xsd:complexType><xsd:element name="addNumber" type="tns:addNumber"/><xsd:complexType name="addNumber"><xsd:sequence><xsd:element minOccurs="0" name="addToNum" type="xsd:long"/></xsd:sequence></xsd:complexType><xsd:element name="addNumberResponse" type="tns:addNumberResponse"/><xsd:complexType name="addNumberResponse"><xsd:sequence><xsd:element maxOccurs="unbounded" minOccurs="0" name="return" type="xsd:long"/></xsd:sequence></xsd:complexType></xsd:schema></wsdl:types><wsdl:message name="addNumber"><wsdl:part element="tns:addNumber" name="parameters">
    </wsdl:part></wsdl:message><wsdl:message name="addNumberResponse"><wsdl:part element="tns:addNumberResponse" name="parameters">
    </wsdl:part></wsdl:message><wsdl:message name="sayHiResponse"><wsdl:part element="tns:sayHiResponse" name="parameters">
    </wsdl:part></wsdl:message><wsdl:message name="sayHi"><wsdl:part element="tns:sayHi" name="parameters">
    </wsdl:part></wsdl:message><wsdl:portType name="HelloWorld"><wsdl:operation name="sayHi"><wsdl:input message="tns:sayHi" name="sayHi">
    </wsdl:input><wsdl:output message="tns:sayHiResponse" name="sayHiResponse">
    </wsdl:output></wsdl:operation><wsdl:operation name="addNumber"><wsdl:input message="tns:addNumber" name="addNumber">
    </wsdl:input><wsdl:output message="tns:addNumberResponse" name="addNumberResponse">
    </wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorld"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="sayHi"><soap:operation soapAction="" style="document"/><wsdl:input name="sayHi"><soap:body use="literal"/></wsdl:input><wsdl:output name="sayHiResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="addNumber"><soap:operation soapAction="" style="document"/><wsdl:input name="addNumber"><soap:body use="literal"/></wsdl:input><wsdl:output name="addNumberResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="HelloWorld"><wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldImplPort"><soap:address location="http://localhost:8080/CXF-WS-1/ws/HelloWorld"/></wsdl:port></wsdl:service></wsdl:definitions>

  set CXF_HOME=D:\apache-cxf-2.3.5

     set JAVA_HOME=D:\Java\jdk1.6.0

     然后命令生成 :wsdl2java -p 包名 -d 生成文件存放的目录(必须存在) -client  http://IP:端口/XX/XXService?wsdl

     cxf会自动生成clent示例代码

 

 

 

axis :将链接保存至 .wsdl 文件,然后通过eclipse右键直接生成,也可通过新建一个工程然后通过url来生成,再拷贝出去。

         实例化proxy类进行调用即可。

 

你可能感兴趣的:(java,spring,webservice,SOAP)