首页 > 其他分享 >Spring3.2 + cxf1.7.3整合

Spring3.2 + cxf1.7.3整合

时间:2023-06-04 15:07:22浏览次数:42  
标签:xml Spring3.2 color spring springframework 整合 org com cxf1.7


参考:[url]http://tsinglongwu.iteye.com/blog/832704[/url]
[color=red][b]测试调试工具介绍 [/b][/color]
[b]1.SoapUI1.6[/b] [url]http://webservices.ctocio.com.cn/tips/263/7817763.shtml[/url]
[b]2.TestMaker[/b] [url]http://webservices.ctocio.com.cn/tips/263/7817763_2.shtml[/url]
[b]3.WebServiceStudio[/b] [url]http://demo.tc/Post/529[/url]
SoapUI支持eclipse插件

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.pandy</groupId>
	<artifactId>webservice</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>webservice Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<spring.version>3.2.1.RELEASE</spring.version>
		<tomcat.version>2.1-SNAPSHOT</tomcat.version>
		<junit.version>3.8.1</junit.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<!-- Spring jars -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-asm</artifactId>
			<version>3.1.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2.2</version>
		</dependency>

		<!-- CXF jars -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-core</artifactId>
			<version>2.7.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-api</artifactId>
			<version>2.7.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>2.7.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>2.7.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.neethi</groupId>
			<artifactId>neethi</artifactId>
			<version>3.0.0</version>
		</dependency>


	</dependencies>
	<build>
		<finalName>webservice</finalName>
	</build>
</project>



[color=red][b]提供服务端[/b][/color]


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

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath: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>/*</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

</web-app>



非注解: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:p="http://www.springframework.org/schema/p"
	xmlns:jaxws="http://cxf.apache.org/jaxws" 
	xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.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-servlet.xml" />
       <!--  心版本去掉cxf/cxf-extension-soap.xml-->
	<!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> -->

	<bean id="hello" class="com.webservices.impl.HelloWorldImpl" />
	<jaxws:endpoint id="helloWorld" implementor="#hello"
		address="/HelloWorld" />
</beans>


[b]<jaxws:endpoint>有三个属性id, implementor和address。[/b]


“[color=red][b]id[/b][/color]”指定这个Bean在Spring上下文中唯一的标识。


“[color=red][b]implementor[/b][/color]”指定了这个Web Service的实现类。


“[color=red][b]address[/b][/color]”指定了服务在Web服务器上发布的地址。这个地址可以包含ip和端口的完整地址,也可以是只包含相对路径的地址。



注解: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:p="http://www.springframework.org/schema/p"
	xmlns:jaxws="http://cxf.apache.org/jaxws" 
	xmlns:cxf="http://cxf.apache.org/core"
	xmlns:context="http://www.springframework.org/schema/context"
	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
	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-servlet.xml" />

	<context:annotation-config />
	<context:component-scan base-package="com" />

	<jaxws:endpoint id="helloWorld" implementor="#helloWorldImpl"
		address="/HelloWorld" />
</beans>


[b]注意:[/b] 同时应该在实现类加上@Component注解.




接口:使用@WebService注解,表明这个接口就是对外提供的服务.


package com.webservices;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.webservices.bean.User;
/**
 * 将要用来发布的接口
 * @author pandy
 *
 */
@SuppressWarnings("restriction")
@WebService
public interface IHelloWorld {
	// 加入WebParam注解,以保证xml文件中参数名字的正确性
	String sayHi(@WebParam(name = "text") String text);
	String sayHiToUser(User user);

}




实现类:使用@WebService(endpointInterface = "com.webservices.IHelloWorld", serviceName = "HelloWorld")注解,表明这个是IHelloWorld服务的实现类,并提供发布到外部,服务名字是HelloWorld


package com.webservices.impl;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.jws.WebService;
import com.webservices.IHelloWorld;
import com.webservices.bean.User;
/**
 * 接口的实现类
 * @author pandy
 *
 */
@SuppressWarnings({ "restriction" })
@WebService(endpointInterface = "com.webservices.IHelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements IHelloWorld {
	Map<Integer, User> users = new LinkedHashMap<Integer, User>();

	@Override
	public String sayHi(String text) {
		System.out.println("sayHi called");
		return "Hello " + text;
	}

	@Override
	public String sayHiToUser(User user) {
		System.out.println("sayHiToUser called");
		users.put(users.size() + 1, user);
		return "Hello " + user.getName();
	}

}


也可以使用@Component去注解实现类,才能使用注解方式.




运行工程,进入:[url]http://localhost:8080/webservice/HelloWorld?wsdl[/url] ,得到的wsdl


<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.webservices.com/"
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http"
	xmlns:ns1="http://webservices.com/" name="HelloWorld" targetNamespace="http://impl.webservices.com/">
	<wsdl:import
		location="http://localhost:8080/webservice/HelloWorld?wsdl=IHelloWorld.wsdl"
		namespace="http://webservices.com/"></wsdl:import>
	<wsdl:binding name="HelloWorldSoapBinding" type="ns1:IHelloWorld">
		<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="sayHiToUser">
			<soap:operation soapAction="" style="document" />
			<wsdl:input name="sayHiToUser">
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="sayHiToUserResponse">
				<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/webservice/HelloWorld" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>


则表示服务端的webservice已经部署成功,就等着客户端来访问.



[color=red][b]客户端:[/b][/color]


pom.xml, [b]类似服务端的pom.xml,只是名字有差别而已[/b]


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.pandy</groupId>
	<artifactId>webserviceclient</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>webserviceclient Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<spring.version>3.2.1.RELEASE</spring.version>
		<tomcat.version>2.1-SNAPSHOT</tomcat.version>
		<junit.version>3.8.1</junit.version>
	</properties>

	<dependencies>
		......
	</dependencies>
	<build>
		<finalName>webserviceclient</finalName>
	</build>
</project>



使用命令得到基本的类:


[color=darkblue][b]wsdl2java -d /mnt/d/vmware_shared_folder/workspace/webserviceclient/src/main/java -p com.websercices.client -frontend jaxws21 http://localhost:8080/webservice/HelloWorld?wsdl[/b][/color]



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:p="http://www.springframework.org/schema/p"
	xmlns:jaxws="http://cxf.apache.org/jaxws" 
	xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schema/jaxws.xsd">

	<bean id="client" class="com.websercices.client.IHelloWorld" factory-bean="clientFactory"
		factory-method="create" />

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


[color=red][b]注意:[/b][/color]


1. org.apache.cxf.jaxws.JaxWsProxyFactoryBean要知道提供服务的接口和URL.


2. <property name="address" value="http://localhost:8080/webservice/HelloWorld" />中的/HelloWorld要与服务器端applicationContext.xml中的<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />的address属性对应。



建立一个测试类:


package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.websercices.client.IHelloWorld;  
public class Test {  
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");  
        IHelloWorld server = (IHelloWorld) ctx.getBean("client");  
        String result = server.sayHi("猪头!");  
        System.out.println(result); 
    }  
}



运行客户端测试类,得到结果: Hello 猪头!


标签:xml,Spring3.2,color,spring,springframework,整合,org,com,cxf1.7
From: https://blog.51cto.com/u_3871599/6410658

相关文章

  • Spring整合mybatis使用xml配置事务
    自己准备开始教授Java相关的技术,Spring框架是必须让学生学习的框架之一。里面有一个事务的配置以前刚学习Spring框架的时候有接触过,不过已经过了很多年,很多东西都已经忘记。现在再来回忆一下如何使用Spring框架类配置事务。使用到的maven坐标如下:<dependencies>    <......
  • Spring 3.0.5+MyBatis3.0.4整合非完全例子
    基于注解的mybatis和spring整合:[url]http://huangmin001.iteye.com/blog/1185806[/url][color=red]这个文章说的很详细,很值得一看[/color].Maven+SpringMVC+Mybatis【绝非原创,单纯整理】【四】:[url]http://playgod1984.iteye.com/blog/984113[/ur......
  • 基于SqlSugar的开发框架循序渐进介绍(30)-- 整合客户关系管理系统模块功能
    以前在随笔《Winform开发框架之客户关系管理系统(CRM)的开发总结系列1-界面功能展示 》的几篇随笔中介绍过基于WInform开发框架开发的CRM系统,系统的功能主要也是围绕着客户相关信息来进行管理的,经过一些客户的定制应用,以及框架各种功能的完善,系统也已经很完善了,不过不同的CRM侧重......
  • Spring Boot 整合 分布式搜索引擎 Elastic Search 实现 我附近的、酒店竞排
    文章目录⛄引言一、我附近的酒店⛅需求分析⚡源码编写二、酒店竞价排名⌚需求分析⏰修改搜索业务✅效果图⛵小结⛄引言本文参考黑马分布式ElasticsearchElasticsearch是一款非常强大的开源搜索引擎,具备非常多强大功能,可以帮助我们从海量数据中快速找到需要的内容一、我附近的酒......
  • 整合ChatGPT与Flutter高级技术
    整合ChatGPT与Flutter高级技术在当今快节奏的数字时代,人们对智能化应用的需求越来越高。ChatGPT是一种基于人工智能的语言模型,而Flutter是一种快速开发移动应用程序的框架。本文将介绍如何将这两种技术整合在一起,从而为用户提供更好的智能化移动应用体验。ChatGPT概述ChatGPT(Genera......
  • Sentinel整合Apollo进行规则持久化(二)
      【Sentinel整合Apollo进行规则持久化】对Sentinel的代码进行了改造,【Apollo配置中心管理后台的详解】对Apollo管理后台进行了讲解。今天就最终实现:Sentinel整合Apollo进行规则持久化 0x01:先把Apollo配置中心启动主要启动如下三个服务:apollo-configservice:提供配置获取接口,提供......
  • 使用ILMerge将多个DLL整合进winform程序
    ILMerge是微软开发的工具用于将多个dll、exe等合并,项目已经开源在github 命令行方式使用范例:ILMerge.exe /ndebug/target:winexe/out:output.exe1.exe1.dll2.dll3.dll......
  • IDEA 创建JavaWeb项目(不依赖 springboot)手动整合 SSM框架
    目录 1.创建一个Maven项目2.创建webapp和WEB-INF目录即可3.设置web.xml文件和web文件目录4.导入相关ssm框架的pom依赖文件5.创建对应的包结构:6.添加配置文件(配置mybatis、spring、springMvc等配置文件)7.web.xml配置文件8. 配置本地Tomcat运行9.打包发布1.创建一个Mav......
  • Springboot整合EasyPOI操作Excel文件
    配合官网阅读本文源码POI官网文档效果图:本文只做一个案列的操作:pom依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--EasyPoi导入导......
  • springboot整合shiro实现认证授权源码
    shiro-admin介绍springboot整合shiro实现前后端分离架构(swagger文档协调前端开发)源码地址:https://gitee.com/liujinxin_ark/shiro-admin软件架构架构说明springboot+shiro+mysql+swagger使用说明运行项目后访问http://localhost:8080/doc.html即可进入swagger接口文档界......