`

spring集成实现webService

 
阅读更多

1、  配置web.xml

 

见文件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</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
 	<context-param>
     	<param-name>contextConfigLocation</param-name>
     	<param-value>WEB-INF/classes/applicationContext.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>/webservice/*</url-pattern>
  	</servlet-mapping>
  
  
  
  
  <!-- 字符过滤器 -->  
    <filter>  
        <filter-name>encoding</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
      
      
    <filter-mapping>  
        <filter-name>encoding</filter-name>  
        <url-pattern>*.jsp</url-pattern>  
    </filter-mapping>  
    <filter-mapping>  
        <filter-name>encoding</filter-name>  
        <url-pattern>*.html</url-pattern>  
    </filter-mapping>  
    <filter-mapping>  
        <filter-name>encoding</filter-name>  
        <url-pattern>*.do</url-pattern>  
    </filter-mapping>  
    <filter-mapping>  
        <filter-name>encoding</filter-name>  
        <url-pattern>*.action</url-pattern>  
    </filter-mapping> 
    <filter-mapping>  
        <filter-name>encoding</filter-name>  
        <url-pattern>*.jsp</url-pattern>  
    </filter-mapping>  
    <filter-mapping>  
        <filter-name>encoding</filter-name>  
        <url-pattern>*.html</url-pattern>  
    </filter-mapping>  
    <filter-mapping>  
        <filter-name>encoding</filter-name>  
        <url-pattern>*.do</url-pattern>  
    </filter-mapping>  
    <filter-mapping>  
        <filter-name>encoding</filter-name>  
        <url-pattern>*.3g</url-pattern>  
    </filter-mapping>   
  
</web-app>

 

2、  配置applicationContext.xml

 

见文件applicationContext.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.hsy.server.HelloWorldImpl"
             address="/helloWorld" />

     <bean id="client" 
     		class="com.hsy.server.HelloWorld" 
     		factory-bean="clientFactory" 
     		factory-method="create"/>

     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
            <property name="serviceClass" value="com.hsy.server.HelloWorld"/>
            <property name="address" value="http://localhost:8080/cxf/webservice/helloWorld"/>
     </bean>
     
</beans>

 

3、  修改客户端代码

 

见文件HelloWorldClient.java

package com.hsy.client;

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

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.hsy.pojo.User;
import com.hsy.server.HelloWorld;

public class HelloWorldClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//Resource resource= new FileSystemResource("F:/workspaces4me2013/.metadata/.me_tcat/WEB-INF/classes/applicationContext.xml");   
		//BeanFactory factory= new XmlBeanFactory(resource ); 
		ApplicationContext factory = new ClassPathXmlApplicationContext("/applicationContext.xml");
		HelloWorld client = (HelloWorld)factory.getBean("client");
        User user1 = new User();
        user1.setName("马");
        user1.setDescription("怀念马");
        User user2 = new User();
        user2.setName("恩");
        user2.setDescription("怀念恩");
        List<User> userList= new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);
        String[] res = client.SayHiToUserList(userList);
        System.out.println(res[0]);
        System.out.println(res[1]);  
        
    }

}

 

4、  启动tamcat发布webService

 

然后在浏览器输入地址:http://localhost:8080/cxf/webservice/helloWorld?wsdl

5、  运行客户端代码访问webService

右键 run as 选择java application,控制台打印

Ok,客户端访问也成功了。

 此篇实现了webService服务的发布以及在本工程下的客户端调用服务的示例

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics