`
BBjava
  • 浏览: 123829 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

cxf-spring

阅读更多
webservice如今开发运用已经很容易了,这都得益于巨头们的努力.众多框架:cxf……
这个示例也是用的cxf,这是我的第一个webservice 例子,jar包这里没有上传,可以到官网下载。
HelloService.java代码:
package org.iteye.bbjava.ws.service;

import javax.jws.WebService;

@WebService
public interface HelloService {
	String sayHi(String text);
}



HelloServcieImpl.java代码:
package org.iteye.bbjava.ws.service.impl;

import javax.jws.WebService;

import org.iteye.bbjava.ws.service.HelloService;

@WebService(endpointInterface = "org.iteye.bbjava.ws.service.HelloService")
public class HelloServiceImpl implements HelloService {

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

}


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-servlet.xml" />

	<jaxws:endpoint id="helloService" implementor="#hello"
		address="/HelloServiceImpl" />
	<bean id="hello" class="org.iteye.bbjava.ws.service.impl.HelloServiceImpl" />
</beans>


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>ws-spring</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/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>
</web-app>




配置的说明:
<servlet-name>CXFServlet</servlet-name>
	<url-pattern>/*</url-pattern>
上面的url-pattern的作用比较重要,因为CXFServlet会“过虑”关相请求。我们就以上面的代码为例,在浏览器中输入http://localhost:8080/ 会进入webservice相关页面,也就是说在正常的main.jsp它进不去了。

那如何解决呢?
请看:web.xml:
引用
<servlet-name>CXFServlet</servlet-name>
	<url-pattern>/service/ *</url-pattern>



HelloServiceImpl.java:
@WebService(endpointInterface = "org.iteye.bbjava.ws.service.HelloService",targetNamespace = "http://localhost:8080/service")



到此服务端的内容就结束了。
下面是客户端了,

package org.iteye.bbjava.ws;

import org.iteye.bbjava.ws.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ClientTest {

	private ClientTest() {
	}

	public static void main(String args[]) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "applicationContext.xml" });
		HelloService client = (HelloService) context.getBean("helloService");
		String response = client.sayHi("zhang");
		System.out.println("Response: " + response);
	}
}


客户端的:
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-2.0.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

	<bean id="helloService" class="org.iteye.bbjava.ws.service.HelloService"
		factory-bean="clientFactory" factory-method="create" />
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass"
			value="org.iteye.bbjava.ws.service.HelloService" />
		<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
	</bean>
</beans>

配置的说明:
引用
在client.xml:
<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
改为
<property name="address" value="http://localhost:8080/ws-spring/service/HelloServiceImpl" />




服务端的applicationContext.xml 细则说明:
引用


<jaxws:endpoint id="helloService" implementor="#hello"  
        address="/HelloServiceImpl" />

jaxws:endpoint 的属性implementor说明 helloService这个接口由hello这个bean实现的。
官方的解释:http://cxf.apache.org/docs/jax-ws-configuration.html,上面写得很清楚,英文好的话建议看一下。



客户端的applicationContext.xml细则说明:
引用



<bean id="helloService" class="org.iteye.bbjava.ws.service.HelloService"
		factory-bean="clientFactory" factory-method="create" />
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass"
			value="org.iteye.bbjava.ws.service.HelloService" />
		<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
	</bean>


上面两行。
两个bean共同组成了一个webserive的接口客户端的核心,其中
<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />

指出了,服务端的地址。(文字不好组织)


分享到:
评论

相关推荐

    CXF-Spring相关jar包

    在【压缩包子文件的文件名称列表】中,"cxf-spring"可能包含的是用于集成CXF与Spring的必要jar包,如cxf-spring-integration.jar,这个文件通常包含了CXF与Spring集成所需的类和资源,使得开发者能够在Spring环境下...

    webservice-cxf-spring-jar.zip

    【标题】"webservice-cxf-spring-jar.zip" 是一个包含了使用Apache CXF与Spring框架集成开发Web服务的Java库集合。这个压缩包提供了一整套必要的JAR文件,以便于开发者在他们的项目中快速搭建和运行基于CXF的Web服务...

    CXF-Spring-Client-J.zip

    "CXF-Spring-Client-J.zip"这个压缩包文件显然提供了一个关于如何在Spring环境中配置和使用CXF客户端的示例。 首先,我们需要理解Spring和CXF的整合是如何工作的。Spring通过其ApplicationContext容器管理CXF客户端...

    webservice-CXF-spring+maven

    【标题】"webservice-CXF-spring+maven" 指的是使用Apache CXF框架,结合Spring和Maven构建Web服务。Apache CXF是一个开源的Java框架,它允许开发人员创建和消费各种Web服务,包括SOAP和RESTful类型。Spring框架则...

    CXF-spring-server.zip

    标题"CXF-spring-server.zip"表明这是一个关于使用Spring框架整合Apache CXF的服务器端示例项目。Apache CXF是一个开源的Java服务框架,它允许开发者创建和消费各种Web服务,而Spring框架则是一个广泛使用的应用框架...

    cxf-spring 服务端and客户端

    【标题】"cxf-spring 服务端and客户端"揭示了这个项目是关于Apache CXF框架与Spring框架的集成,用于构建服务端和客户端应用程序。Apache CXF是一个开源的Java框架,它允许开发者创建和消费Web服务,而Spring框架则...

    apache-cxf-3.1.1跟3.1.6所有jar包

    6. **Spring集成**:CXF与Spring框架深度集成,使得服务的配置和管理变得更加方便,同时也易于与其他Spring生态系统的组件配合使用。 7. **安全性**:CXF提供了多种安全机制,如基本认证、Digest认证、OAuth、SSL/...

    apache-cxf-2.5.2

    Apache CXF 是一个开源的Java框架,...此外,他们还需要了解如何在Spring框架中集成CXF,以便更好地管理服务生命周期和依赖注入。对于更高级的使用,开发者可能需要深入研究数据绑定、安全性和性能优化等方面的知识。

    cxf+spring所需的jar包

    - Spring的相关jar:spring-context.jar、spring-beans.jar、spring-core.jar等,提供Spring的核心功能。 - 可能还包括其他依赖,如jaxb、wsdl4j、httpclient等,这些都是CXF和Spring工作所需的基础库。 5. 使用...

    apache-cxf-3.3.3.zip

    此外,CXF还集成了Spring框架,使得集成到Spring应用中变得简单。 6. **代码生成**:CXF可以自动生成客户端和服务端代码,只需提供WSDL文件,即可生成相应的Java代码。这对于快速开发和调试非常有帮助。 7. **性能...

    apache-cxf-3.5.0.zip

    - **易于集成**: CXF可以很好地与Spring框架集成,使得服务配置变得更加简单。 - **丰富的工具支持**: 提供了CXF wsdl2java工具,可以从WSDL生成Java代码,反之亦然,方便开发和调试。 - **强大的异常处理**: CXF...

    cxf至少需要的jar包下载,集成Spring cxf jar下载

    cxf与spring集成 需要最少的jar如下: cxf-2.3.3.jar geronimo-annotation_1.0_spec-1.1.1.jar geronimo-jaxws_2.2_spec-1.0.jar geronimo-stax-api_1.0_spec-1.0.1.jar geronimo-ws-metadata_2.0_spec-1.1.3.jar ...

    cxf-2.4.2 jar包

    10. **与其他框架的集成**:CXF可以很容易地与Spring框架集成,允许开发者利用Spring的依赖注入和配置管理优势。 在"CXF-2.4.2"的jar包中,可能包含CXF框架的所有核心组件和服务,如服务器端和客户端处理程序、数据...

    apache-cxf-3.1.6.zip官网完整包

    这个"apache-cxf-3.1.6.zip"是Apache CXF的3.1.6版本的官方完整包,发布于2016年4月14日,包含了与Spring框架的集成支持。 **Apache CXF核心特性:** 1. **Web服务实现**:CXF提供了基于Java API for Web Services...

    apache-cxf-3.1.6所有jar包

    - **其他依赖库**:还包括了与CXF协同工作的第三方库,如Spring框架、Apache Commons库等。 在实际开发中,根据项目需求,可以选择性地引入这些jar包,或者通过Maven或Gradle等构建工具来管理依赖,以减小项目的...

    apache-cxf-2.6.2.zip

    CXF的强项在于其易于使用和集成,它可以与Spring框架无缝结合,使得配置和服务实现变得简单。 在"apache-cxf-2.6.2.zip"压缩包中,你将找到以下主要内容: 1. **JAR文件**:这些是CXF的核心库,包括cxf-api.jar、...

    apache-cxf-2.7.7.zip

    8. **与其他框架集成**:CXF可以很好地与Spring框架集成,使得服务的配置和管理更加方便。此外,它也支持OSGi环境。 9. **动态客户端**:CXF的动态客户端可以在运行时根据WSDL动态生成客户端代码,降低了对服务端...

    cxf_spring全部jar包

    neethi-3.0.2.jar,spring-asm-3.0.7.RELEASE.jar,spring-asm-3.0.7.RELEASE.jar,spring-beans-3.0.7.RELEASE.jar,spring-context-3.0.7.RELEASE.jar,spring-core-3.0.7.RELEASE.jar,spring-expression-3.0.7....

Global site tag (gtag.js) - Google Analytics