cxf spring开发web service-1 Hello World
一.服务端
0. 新建一个web工程
1. web.xml配置spring和cxf servlet
2. 空的applicationContext.xml 的 applicationContext-ws.xml
3. 服务接口和实现类
4. 在applicationContext-ws.xml中配置发布service
5. lib快照,很多没用到的一股脑塞进来了,管理得很乱
6. 部署到tomcat运行,访问http://localhost:9080/cxf-security/ws/hello?wsdl
二.客户端
k1. 通过cxf带的wsdl2java工具生成服务对应的接口类(现在直接从服务端复制过来就行)
k2. 客户端类,运行就可以了
三.spring配置客户端
sk1. spring配置文件
sk2. 客户端类,运行就可以了
四,
CXF密码验证_服务端和客户端配置
CXF 密码权限控制 SOAP报头处理
要加的jar:
wss4j-x.x.x.jar(1.5.8)
xmlsec-x.x.x.jar(1.4.3)
cxf-rt-ws-secutity-x.x.x.jar(2.2.8)
一.服务端
1. web.xml配置spring和cxf servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
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/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext.xml,classpath*:/applicationContext-ws.xml</param-value>
</context-param>
<!--Spring ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- CXF 配置 -->
<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>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
2. 空的applicationContext.xml 的 applicationContext-ws.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>
applicationContext-ws.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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>
3. 服务接口和实现类
IHelloService.java
用@WebService注解标注为webservice
package zhch.illq.service;
import javax.jws.WebService;
@WebService
public interface IHelloService {
public String sayHello(String user);
}
HelloService.java 普通的java类,实现接口
package zhch.illq.service.impl;
import zhch.illq.service.IHelloService;
public class HelloService implements IHelloService {
@Override
public String sayHello(String user) {
return "Hello," + user + "!";
}
}
4. 在-ws.xml中配置发布service
上面命名空间多了两个,schemaLocation多了四个,
不全可能报错:
引用
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'jaxws:endpoint'.
而且xml有时比较诡异,如果加了jaxws的声明依然报错,可以试下把所有字母手敲一遍,防止错误的空格之类看不出来的问题.还有地址很长,不要敲错,最好找个正确的文件复制!!!
然后:
首先要导入cxf的三个文件,再然后配置endpoint和service bean
<?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://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<!-- jax-ws endpoint定义 -->
<jaxws:endpoint address="/hello">
<jaxws:implementor ref="helloService" />
</jaxws:endpoint>
<!-- WebService的实现Bean定义 -->
<bean id="helloService" class="zhch.illq.service.impl.HelloService" />
</beans>
5. lib快照,很多没用到的一股脑塞进来了,管理得很乱
XmlSchema-1.4.5.jar
aa.txt
antlr-2.7.6.jar
aopalliance.jar
apache-ant-zip.jar
asm-2.2.3.jar
c3p0-0.9.1.2.jar
commons-beanutils.jar
commons-codec-1.3.jar
commons-collections-3.1.jar
commons-email-1.1.jar
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
commons-validator-1.3.0.jar
cxf-api-2.2.9.jar
cxf-common-schemas-2.2.9.jar
cxf-common-utilities-2.2.9.jar
cxf-rt-bindings-soap-2.2.9.jar
cxf-rt-bindings-xml-2.2.9.jar
cxf-rt-core-2.2.9.jar
cxf-rt-databinding-jaxb-2.2.9.jar
cxf-rt-frontend-jaxws-2.2.9.jar
cxf-rt-frontend-simple-2.2.9.jar
cxf-rt-transports-http-2.2.9.jar
cxf-rt-transports-http-jetty-2.2.9.jar
cxf-rt-ws-addr-2.2.9.jar
cxf-tools-common-2.2.9.jar
dom4j-1.6.1.jar
ehcache-1.6.0-beta3.jar
fckeditor-2.4.1.jar
freemarker-2.3.15.jar
geronimo-activation_1.1_spec-1.0.2.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-javamail_1.4_spec-1.6.jar
geronimo-jaxws_2.1_spec-1.0.jar
geronimo-servlet_2.5_spec-1.2.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
hibernate-core-3.3.1.jar
hibernate-ehcache-3.3.1.GA.jar
htmlparser-1.6.jar
javassist-3.9.0.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.13.jar
jcaptcha-all-1.0-RC6.jar
jcl-over-slf4j-1.5.2.jar
jeecms-2.4.2-final.jar
jeecms-common-2.4.2.jar
jeecms-core-2.4.2.jar
jetty-6.1.23.jar
jetty-util-6.1.23.jar
jsonplugin-0.33.jar
jta-1.1.jar
log4j-over-slf4j-1.5.6.jar
logback-classic-0.9.11.jar
logback-core-0.9.11.jar
lucene-core-3.0.0.jar
mail.jar
mysql.jar
neethi-2.0.4.jar
ognl-2.6.11.jar
quartz-1.6.5.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
slf4j-api-1.5.2.jar
spring-aop-2.5.6.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-context-support-2.5.6.jar
spring-core-2.5.6.jar
spring-jdbc-2.5.6.jar
spring-orm-2.5.6.jar
spring-tx-2.5.6.jar
spring-web-2.5.6.jar
struts-core-2.1.6.jar
struts-spring-plugin-2.1.6.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.9.jar
xml-resolver-1.2.jar
xstream-1.3.2-20090405.073822-1.jar
xwork-2.1.2.jar
6. 部署到tomcat运行,访问http://localhost:9080/cxf-security/ws/hello?wsdl
端口和项目名按照实际修改下
二.客户端
k1. 通过cxf带的wsdl2java工具生成服务对应的接口类(现在直接从服务端复制过来就行)
k2. 客户端类,运行就可以了
package zhch.illq.client;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import zhch.illq.service.IHelloService;
public class HelloClient {
// 两个参数分别是 web service的targetNamespace和name
// 默认就是(http://+包名的倒装)和(类名),可以在@WebService()注解中设置
// 最后有/的话不能忘了,一个字符也不能差
private static final QName SERVICE_NAME = new QName("http://service.illq.zhch/", "IHelloService");
// 这个是targetNamespace和name+"Port"
private static final QName PORT_NAME = new QName("http://service.illq.zhch/", "IHelloServicePort");
private HelloClient() {
}
public static void main(String args[]) throws Exception {
Service service = Service.create(SERVICE_NAME);
// Endpoint Address
// 这个只要前面对,后面多上一堆也可以,很是奇怪
// 这样也行??http://localhost:9080/cxf-security/ws/hellosllooiehfkdjfajs
String endpointAddress = "http://localhost:9080/cxf-security/ws/hello";
// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
IHelloService helloService = service.getPort(IHelloService.class);
System.out.println(helloService.sayHello("World"));
}
}
三.spring配置客户端
sk1. spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="client" class="zhch.illq.service.IHelloService"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="zhch.illq.service.IHelloService"/>
<property name="address" value="http://localhost:9080/cxf-security/ws/hello"/>
</bean>
</beans>
也可以这样,简单一些
<?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.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="client2"
serviceClass="zhch.illq.service.IHelloService"
address="http://localhost:9080/cxf-security/ws/hello"/>
</beans>
sk2. 客户端类,运行就可以了
package zhch.illq.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zhch.illq.service.IHelloService;
public class HelloClient2 {
public static void main(String args[]) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "client-beans.xml" });
IHelloService client = (IHelloService) context.getBean("client");
System.out.println(client.sayHello("World"));
}
}
分享到:
相关推荐
总的来说,"CXF-Spring-Client-J.zip"这个压缩包提供了一个使用Spring和CXF构建客户端服务调用的实例,帮助开发者了解如何在Spring环境中配置和使用CXF客户端,从而高效地开发和消费Web服务。通过学习和实践这个示例...
@ServiceInterface(name = "HelloWorld", portName = "HelloWorldPort") @WebService(targetNamespace = "http://example.com/ws") public interface HelloWorld { String sayHello(String name); } ``` 接着,...
在本教程中,我们将深入探讨如何使用Apache CXF 3.0与Spring 3.2框架构建一个简单的"Hello World" Web服务实例。这个过程涵盖了关键的技术组件、配置步骤以及可能遇到的问题解决策略。 首先,Apache CXF是一个开源...
<property name="address" value="http://localhost:8080/HelloWorld"/> ``` `serviceClass`属性指定服务实现类,`address`属性设置服务的访问地址。 5. 启动服务器:使用Spring启动应用,CXF会自动部署Web服务。...
@WebService(endpointInterface = "com.ws.cxf.dao.IHelloWorld", serviceName = "helloWorld", targetNamespace = "http://dao.cxf.ws.com/") public class HelloWorldImpl { public String sayHello(String ...
标题 "CXF 2.3 集成Spring3.0入门 HelloWorld" 指向的是一个关于如何在Java项目中使用Apache CXF 2.3版本与Spring 3.0框架进行集成的教程,特别是通过一个简单的"Hello World"应用来演示这个过程。Apache CXF是一个...
Apache CXF是一个流行的开源框架,用于构建和消费Web服务,而Spring是Java开发中的一个全面的框架,提供了强大的依赖注入和管理组件的能力。当我们需要在Web服务中同时处理JSON和XML数据格式时,CXF和Spring的整合就...
【描述】:这篇文章主要介绍如何使用Apache CXF框架进行Web服务开发,通过一个简单的“Hello World”示例来帮助初学者理解CXF的基本用法。 Apache CXF是一个开源的Java框架,主要用于构建和部署SOAP和RESTful Web...
【标题】"CXF的第一个例子helloWorld"是一个基础教程,主要介绍了如何使用Apache CXF框架创建一个简单的Web服务。Apache CXF是一个开源的Java框架,它用于构建和开发Web服务,支持SOAP、RESTful等多种通信协议。这个...
在本文中,我们将深入探讨如何使用Apache CXF 2与Spring 2.5框架来开发Web服务实例。Apache CXF是一个流行的开源项目,它提供了一种简单且强大的方式来实现和消费SOAP和RESTful Web服务。Spring框架则以其模块化、...
在文件列表中的"servicedemo"可能包含了一个完整的示例项目,包括CXF服务接口的定义、实现、Spring配置以及可能的客户端调用代码。通过分析和运行这个示例,你可以更深入地了解CXF的工作原理和使用方法。 总之,CXF...
Spring框架以其强大的依赖注入和面向切面编程能力,为Web Service的开发提供了便利。Spring支持多种Web Service技术,包括JAX-WS。通过Spring,我们可以轻松地集成Web Service客户端和服务端,并且管理其生命周期。 ...
这个简单的“Hello World”示例展示了Apache CXF如何帮助开发者快速搭建Web服务。在实际应用中,你可以扩展这个基础,处理更复杂的业务逻辑,支持各种协议和数据格式,如RESTful API、JSON、XML等。此外,CXF还提供...
**WebService (一) CXF 入门 HelloWorld** 在IT行业中,WebService是一种基于开放标准(如XML、WSDL和SOAP)的互联网通信协议,允许不同...希望这个HelloWorld示例能为你进一步探索CXF和Web服务提供一个良好的起点。
1. **配置 CXF 容器**:在 Spring 配置文件中,我们需要声明一个 CXF 容器,通常是 `JaxWsProxyFactoryBean` 或 `JaxWsServerFactoryBean`,用于创建客户端或服务端的 Web 服务。例如,对于服务端,我们可以这样配置...
【标题】:“实战Web Service —— 使用Apache CXF开发Web服务的教程” 【内容详解】: Web服务是一种基于开放标准的、平台无关的接口技术,它允许不同系统间的应用程序进行交互。Apache CXF是一个开源的Java框架,...
在本例中,我们可能会有一个名为`HelloWorld`的类,它有一个`sayHello()`方法,返回“Hello, World!”这样的简单消息。这个类需要标记为CXF的服务端点,通常使用`@Path`注解来指定REST资源的路径,如`@Path("/hello...
这个项目是一个基础的 HelloWorld 示例,展示了如何在Tomcat 6.0应用服务器上运行一个CXF Web服务。 【描述】中的关键知识点包括: 1. **Apache CXF**: Apache CXF是一个开源服务框架,它允许开发者构建和消费各种...
当CXF与Spring结合时,可以创建高效且灵活的REST接口,便于开发分布式系统。本篇将详细介绍如何利用CXF和Spring进行REST接口的开发。 首先,我们需要在项目中引入CXF和Spring的相关依赖。在Maven工程中,可以在pom....