spring集成实现webService

news/2024/7/5 23:24:01

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服务的发布以及在本工程下的客户端调用服务的示例


http://www.niftyadmin.cn/n/4413334.html

相关文章

The constructor Service(URL, QName, WebServiceFeature[]) is undefined

Service(URL, QName, WebServiceFeature[]) is undefined 原因是CXF自带的javax.xml.service 版本高过 jdk的javax.xml.service。 一个解决办法是&#xff1a; 在使用wsdl2java时&#xff0c;加入参数 -frontend jaxws21 这个问题的消息解释可以在这里看到 &#xff1a; http:/…

wsdl2java用法

wsdl2java用法&#xff1a;wsdl2java -p com -d src -all aa.wsdl-p 指定其wsdl的命名空间&#xff0c;也就是要生成代码的包名:-d 指定要产生代码所在目录-client 生成客户端测试web service的代码-server 生成服务器启动web service的代码-impl 生成web service的实现代码…

StringBuffer内容清空效率比较

在开发程序的时候&#xff0c;经常使用StringBuffer来进行字符串的拼接。如果在循环中来反复的做字符串拼接时&#xff0c;会清空Stringbuffer中的内容&#xff0c;然后再拼接新的字符串信息。 例如&#xff1a; StringBuffer sb new StringBuffer(""); for (UserIn…

MAXIMO与oracle字段对应

FLOATbinary_float浮点数BLOBBLOB二进制大字段CLOBCLOB字符型大字段LONGALNCLOB长描述DATEDate日期&#xff0c;格式&#xff1a;YYYY-MM-DDDATETIMEDate时间&#xff0c;格式&#xff1a;YYYY-MM-DD HH24:MI:SSTIMEDate时间&#xff0c;格式&#xff1a;HH24:MI:SSDECIMALNumb…

hibernate--HQL语法与详细解释

HQL查询&#xff1a;Criteria查询对查询条件进行了面向对象封装&#xff0c;符合编程人员的思维方式&#xff0c;不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性&#xff0c;因此Hibernate将HQL查询方式立为官方推荐的标准查询方式&#xff0c;HQL查…

XMLGregorianCalendar与Date之间转换

import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar;/*** XMLGregorianCalendar类型和Date类型之间的相互转换* author Xin* 2010-…

maximo工作流表

Maximo工作流程相关表结构分析与工作流迁移脚本Maximo版本号&#xff1a;V7110下面整理的资料是参考网上不知哪位仁兄的《浅谈maximo工作流数据迁移》&#xff0c;结合V7版的特点整理的。工作流有关的数据表共有18张&#xff0c;以WF开头&#xff0c;下面是各表的简单说明&…

自定义分页标签结合spring mvc、bootstrap、mybatis、mysql的使用

pager.tld <?xml version"1.0" encoding"UTF-8" ?> <taglib xmlns"http://java.sun.com/xml/ns/j2ee"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://java.sun.com/xml/ns/j2eehttp…