Spring整合Apache CXF(服务端+客户端)

该项目下载路径: 点击打开链接,该项目在线访问路径: 点击打开链接
  1. 创建maven工程: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>yangchao.com</groupId>
	<artifactId>spring-cxf</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-cxf Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
			<scope>test</scope>
		</dependency>
		<!-- cxf.... 引入cxf只需要这两个包 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.1.8</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.1.8</version>
		</dependency>
		<!-- ....spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.0.2.RELEASE</version>
		</dependency>

		<!-- log -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>spring-cxf</finalName>
		<plugins>
			<!--maven构建的jdk版本 -->
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>

			<!--tomcat版本 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<path>/</path>
					<port>80</port>
					<URIEncoding>UTF-8</URIEncoding>
					<useBodyEncodingForURI>true</useBodyEncodingForURI>
				</configuration>
				<version>2.2</version>
			</plugin>
		</plugins>
	</build>
</project>

2.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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	metadata-complete="true" version="3.0">
	<display-name>Spring-Apach-CXF</display-name>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext-ws-server.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>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

3.springXML配置 applicationContext-ws-server.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" xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
	default-lazy-init="true">

	<description>Apache CXF的Web Service配置</description>
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<!-- soap webservice -->
	<jaxws:endpoint implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld" />
</beans>

service:
/**
 * @项目名称:spring-cxf
 * @类名称:HelloWorld
 * @类描述:
 * @创建人:YangChao
 * @联系方式:18629233301@163.com
 * @创建时间:2017年4月26日 下午12:47:50
 * @version 1.0.0
 */
@WebService
public interface HelloWorld {
	String sayHi(String text);
}

/**
 * @项目名称:spring-cxf
 * @类名称:HelloWorldImpl
 * @类描述:
 * @创建人:YangChao
 * @联系方式:18629233301@163.com
 * @创建时间:2017年4月26日 下午12:48:25
 * @version 1.0.0
 */
@WebService(endpointInterface = "demo.spring.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

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


发布之后访问:http://localhost/HelloWorld?wsdl  看到wsdl响应

编写客户端:applicationContext-ws-client.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" xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
	default-lazy-init="true">
	<description>Apache CXF Web Service Client端配置</description>

	<jaxws:client id="helloWorld" serviceClass="demo.spring.service.HelloWorld" address="http://localhost/HelloWorld" />

</beans>

Test:
public class HelloWorldTest {

	@Test
	public void testSayHi() {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "classpath:applicationContext-ws-client.xml" });
		HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
		System.out.println(helloWorld.sayHi("yangchao......"));
	}
}

执行响应结果:Hello yangchao......




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解了您的问题。使用Spring Boot和CXF框架搭建一个Webservice服务端的步骤如下: 1. 首先,在您的项目中添加CXFSpring Boot的依赖,例如: ``` <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.6</version> </dependency> ``` 注意,这里使用的是CXF的JAX-WS模块,您也可以根据需要选择其他模块。 2. 接着,在您的配置文件中添加CXF的配置,例如: ``` @Configuration public class CxfConfig { @Bean public ServletRegistrationBean<CXFServlet> cxfServlet() { return new ServletRegistrationBean<>(new CXFServlet(), "/yourWebserviceUrl/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public YourWebService yourWebService() { return new YourWebService(); } @Bean public Endpoint yourWebServiceEndpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), yourWebService()); endpoint.publish("/YourWebServiceUrl"); return endpoint; } } ``` 这里配置了CXF的Servlet,以及您自己实现的Webservice服务类和Endpoint。注意,您需要将 "/yourWebserviceUrl/" 和 "/YourWebServiceUrl" 替换为您自己的URL。 3. 最后,您可以实现您的Webservice服务类,例如: ``` @Service @WebService(serviceName = "YourWebService") public class YourWebService { @WebMethod public String hello(@WebParam(name = "name") String name) { return "Hello, " + name + "!"; } } ``` 这里实现了一个简单的hello方法,接收一个name参数并返回一个包含该参数的问候语。 完成以上步骤后,您就成功地搭建了一个基于CXFSpring Boot的Webservice服务端。当您在浏览器中输入"http://localhost:8080/YourWebServiceUrl?wsdl"时,您将看到您的Webservice服务的WSDL文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值