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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值