WebService 学习笔记之 集成Spring和Web容器解析

熊孩纸 阅读:209 2021-03-31 22:56:29 评论:0

一.开发环境

我的开发环境是:

MyEclipse 10

Tomcat 6.0

Apache cxf-2.3.0  相关jar包下载地址: http://www.apache.org/dist/cxf/2.3.0/

二.开发步骤

  创建Server

  1.新建一个Web工程WebServiceDemo,并添加apache-cxf-2.3.0相关jar文件,然后建个com.demo包。在包里面创建HelloWorld接口

代码如下

package com.demo; 
 
import javax.jws.WebMethod; 
import javax.jws.WebParam; 
import javax.jws.WebResult; 
import javax.jws.WebService; 
 
@WebService   
public interface HelloWorld { 
	@WebMethod 
	@WebResult String sayHi(@WebParam String text);   
 
} 


 

再创建一个类实现HelloWorld接口

package com.demo; 
 
import javax.jws.WebService; 
 
@WebService 
public class HelloWorldImpl implements HelloWorld { 
 
	public String sayHi(String text) { 
		// TODO Auto-generated method stub 
		return "Hi," + text; 
 
	} 
 
} 


 

然后修改web.xml里面内容

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


 

在WEB-INF下面创建一个beans.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: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.demo.HelloWorldImpl" address="/HelloWorld" /> 
</beans>


 

将Web工程发布到Tomcat里面,然后启动Server

 

  创建Client

  接着创建客户端,在com.demo下面创建client-beans.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: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/schema/jaxws.xsd"> 
 
    <bean id="client" class="com.demo.HelloWorld" 
        factory-bean="clientFactory" factory-method="create" /> 
 
    <bean id="clientFactory" 
        class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> 
        <property name="serviceClass" value="com.demo.HelloWorld" /> 
        <property name="address" value="http://127.0.0.1:8080/WebServiceDemo/HelloWorld" /> 
    </bean> 
 
</beans>


 

在创建测试类Client.java

package com.demo; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
public class Client { 
	public static Client self = new Client(); 
	private HelloWorld client = null; 
 
	private Client() { 
		ApplicationContext context = new ClassPathXmlApplicationContext( 
				new String[] { "com/demo/client-beans.xml" }); 
		client = (HelloWorld) context.getBean("client"); 
 
	} 
 
	public HelloWorld getServer() { 
		return client; 
 
	} 
 
	public String getText(String text) throws Exception { 
		String response = getServer().sayHi(text); 
		return response; 
	} 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		try { 
 
			System.out.println(self.getText("cuiran")); 
		} catch (Exception e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
	} 
 
} 


  然后进行测试运行。如果遇到类似这种错误“AXB 2.1 API is being loaded from the bootstrap classloader”说明你的web工程添加了J2EE 1.4或者是1.5 library. 这样就与jaxb-api-2.2.1.jar

  冲突了,只需删除即可。

最后运行如图

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们