现在的位置: 首页 > 综合 > 正文

Tomcat下通过CXF实现不用注解发布WebService

2013年08月24日 ⁄ 综合 ⁄ 共 4370字 ⁄ 字号 评论关闭

最近开始学习CXF框架下部署WebServices  , 起初都是用注释类或使用JAX-WS的方式如@WebService 等方式来进行和Spring结合发布的WebService

但是后来突然发现这样无形中和Java代码的耦合程度更大了,于是便需找累死xfie的aegis方式进行部署,貌似这类的文章很难找,

虽然找了大概三四天吧不过很幸运,感谢博主http://blog.csdn.net/pengchua/article/details/2740588

本文将通过图文方式进行部署,顺便把和Spring的结合进行讲解一下:

1....................首先看一下我的工程目录,这样会更容易下面的理解

2...............................配置web.xml文件这样所有架构才能启动(之前别忘记加入Spring和CXF的架包)

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

	<!--spring需要加载的配置文件-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext-server.xml
		</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!--
	    cxf服务启动servlet
	-->
	<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>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

3.....................................Spring 配置文件applicationContext.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/context

 			http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<import resource="xml/services.xml" />
</beans>

4................................编写接口文件

package com.interfaces;

import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;

import com.javabean.User;

public interface ServicesInterface {

	//字符串
	public String sayHi(String text);

	//document类型对象
	public Document getDocument();
	
	String sayUserHello(User user);
	 
    List<User> findUsers();
 
    Map<Integer, User> getMapUsers();

}

5...................编写实现类(这些应该都是小儿科了吧)

package com.interfaces;

import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;

import com.javabean.User;

public interface ServicesInterface {

	//字符串
	public String sayHi(String text);

	//document类型对象
	public Document getDocument();
	
	String sayUserHello(User user);
	 
    List<User> findUsers();
 
    Map<Integer, User> getMapUsers();

}

6........................看看User这个JavaBean里面有什么冬冬

package com.javabean;

public class User {
	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}

7........................配置services.xml就是用来部署接口的这个文件,这个很重要,真的很重要(后面就知道为什么了)

<?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:simple="http://cxf.apache.org/simple"
	xmlns:soap="http://cxf.apache.org/bindings/soap"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://cxf.apache.org/core


http://cxf.apache.org/schemas/core.xsd


http://cxf.apache.org/simple

 http://cxf.apache.org/schemas/simple.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" />

	<bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding" />

	<simple:server id="helloservice" serviceClass="com.interfaces.ServicesInterface"
		address="/hello">
		<simple:serviceBean>
			<bean class="com.imp.HelloWorldImpl" />
		</simple:serviceBean>
		<simple:dataBinding>
			<ref bean="aegisBean" />
		</simple:dataBinding>
	</simple:server>
</beans>

注意:上面的配置文件中不仅要注意配置的内容,而且要注意配置的顺序,例如

 http://cxf.apache.org/simple  

http://cxf.apache.org/schemas/simple.xsd

这两个的先后顺序是不能颠倒的,唉,在这里吃好大亏啊,如果颠倒了的话在写下面的标签的时候你回发现根本找不到simple的标签,所以一定要注意

8.............................如果一切顺利的话那么就来启动服务器来看看我们的成果吧

http://localhost:8080/aegis_CXF/services/hello?wsdl

呵呵最喜欢看到下面这张图了

9......................给自己点鼓励(恭喜恭喜呵呵)

抱歉!评论已关闭.