Build Web Service by CXF
CXF(http://cxf.apache.org/)
Apache CXF is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.
Web Services Standards Support: CXF supports a variety of web service standards including SOAP, the Basic Profile, WSDL, WS-Addressing, WS-Policy, WS-ReliableMessaging, and WS-Security.
Frontends: CXF supports a variety of "frontend" programming models. CXF implements the JAX-WS APIs (version 2.0 will be TCK compliant). It also includes a "simple frontend" which allows creation of clients and endpoints without annotations. CXF supports both contract first development with WSDL and code first development starting from Java.
Ease of use: CXF is designed to be intuitive and easy to use. There are simple APIs to quickly build code-first services, Maven plug-ins to make tooling integration easy, JAX-WS API support, Spring 2.0 XML support to make configuration a snap, and much more.
Binary and Legacy Protocol Support: CXF has been designed to provide a pluggable architecture that supports not only XML but also non-XML type bindings, such as JSON and CORBA, in combination with any type of transport.
Server Side
----------------------------------------
1) Dependencies
aopalliance-1.0.jar
FastInfoset-1.2.3.jar
spring-beans-2.5.5.jar
spring-context-2.5.5.jar
spring-core-2.5.5.jar
spring-web-2.5.5.jar
commons-logging-1.1.1.jar
cxf-2.2.2.jar
jaxb-impl-2.1.9.jar
jdk6.earlier
neethi-2.0.4.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.5.jar
asm-2.2.3.jar
log4j.jar
2)
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
3)
import javax.jws.WebService;
@WebService(endpointInterface = "demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "Hello " + text;
}
}
4) beans.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="demo.spring.HelloWorldImpl" address="/HelloWorld"/>
</beans>
5) web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-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>
6) log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="RollingFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="Append" value="false"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="File" value="c:/tmp/test.log"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n"/>
</layout>
</appender>
<!--
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %p - %l - %m%n"/>
</layout>
</appender>
-->
<!--
<logger name="demo.spring" additivity="false">
<level value="info"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
-->
<root>
<priority value="info"/>
<!--<appender-ref ref="ConsoleAppender"/>-->
<appender-ref ref="RollingFileAppender"/>
</root>
</log4j:configuration>
7) Deploy to Tomcat
set appName=CxfHello
set containerHOME=D:\ProgramFiles\tomcat-5.5.17
cd web
jar cvf %appName%.war *.*
move %appName%.war ..
cd..
copy %appName%.war %containerHOME%\webapps
Client side
----------------------------------------
1) Dependencies
The same as the server
2) server.properties
server.host=my.test.domain.com
server.port=8088
3) client-beans.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="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:server.properties</value>
</list>
</property>
</bean>
<!-- class="demo.spring.HelloWorld" -->
<bean id="hello" factory-bean="helloFactory" factory-method="create"/>
<bean id="helloFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="demo.spring.HelloWorld"/>
<property name="address" value="http://${server.host}:${server.port}/CxfHello/HelloWorld"/>
</bean>
</beans>
4) Web Service interface
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
5) Invoke web service
import demo.spring.HelloWorld;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Client {
public static void main(String args[]) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "demo/spring/client/client-beans.xml" });
HelloWorld client = (HelloWorld) context.getBean("hello");
String response = client.sayHi("Joe");
System.out.println(response);
}
}
Note:
1) 如果返回/传递的参数是对象,要实现get/set 方法
2) 如果有方法重载,需要用@WebMethod标注一下operationName赋值为不同值
@WebService
public interface HelloWorld {
String sayHi(String text);
Double plus(double a, double b);
@WebMethod(operationName="doIt1")
DummyObject doIt(int id, String name);
@WebMethod(operationName="doIt2")
Apple doIt(DummyObject obj);
}
分享到:
相关推荐
实战Web Service 之 CXF 实战Web Service 之 CXF
实战Web Service with CXF cxf非常好的ppt教程
CXF是一个流行的开源框架,专门用于开发和实现Web服务。CXF框架简化了Web服务的创建过程,提供了高度灵活且易于使用的API,使得开发者可以快速上手。 **CXF框架概述** CXF(Codehaus XFire的后继者)是一个Java库,...
Apache CXF是一个开源的Java框架,它用于构建和开发Web服务。这个框架提供了一种灵活的方式来进行服务的创建、发布和调用。"Code-First"是CXF的一个重要概念,意味着开发者首先编写Java代码,然后CXF会自动生成相应...
在"Apache CXF开发Web Service理解CXF Frontends之Contract-First"的主题中,我们将深入探讨以下几个关键知识点: 1. **Web服务基础**:Web服务是一种通过网络进行通信的应用程序接口,使用标准协议如SOAP(简单...
在Java世界中,Apache CXF是一个广泛使用的框架,它支持创建和消费Web服务。本篇将深入讲解如何利用Apache CXF实现一个简单的"Hello World" Web服务DEMO。 首先,我们需要了解Web服务的基本概念。Web服务是通过SOAP...
【标题】中的"Apache CXF Web Service Development"指的是使用Apache CXF框架进行Web服务开发的过程。这通常包括了创建服务接口、实现服务逻辑、配置服务端点、以及发布和调用服务等步骤。源码部分可能包含了示例...
1. 安装和配置CXF库:确保项目的`pom.xml`或`build.gradle`文件中添加了CXF的依赖。 2. 生成Java客户端代码:使用CXF的命令行工具或Maven插件,从`WeatherWS.wsdl`生成Java源代码。 3. 编写测试代码:在`MainTest....
在"Web Service学习-CXF开发Web Service实例demo(一)"中,我们将专注于SOAP Web Service的实现。以下是一些关键步骤: 1. **创建服务接口**:首先,你需要定义一个Java接口,这个接口会成为你的Web Service接口。...
标题"Web Service CXF Spring集成"表明我们将探讨如何在Spring环境中利用Apache CXF来创建和整合Web服务。首先,你需要在项目中引入CXF和Spring的相关依赖。通常,这可以通过在Maven或Gradle的配置文件中添加相应的...
提供的`cxf_service`文件可能包含了CXF运行时所需的集合,如JAXB绑定类、WSDL文件、XSD schema等。这些文件对于构建和运行Web服务至关重要。 **六、客户端调用** 使用Spring的JAX-WS客户端代理工厂...
1、解压cxf-spring-server文件后发布到tomcat服务器上(服务端) 2、解压cxf-spring-client文件后导入eclipse中运行...说明:在Web Server的服务端及客户端都添加了拦截器,模拟用户登录权限校验
### 使用Apache CXF实现Web服务的关键知识点 #### 一、Web服务技术的发展与现状 自Web服务技术出现以来,它已经在完善自身并找到在实际应用中的正确定位方面取得了长足的进步。随着规范的成熟,Web服务技术凭借其...
本篇将深入探讨如何利用CXF来开发Web Service及其客户端,以及相关的重要知识点。 ### 1. CXF简介 Apache CXF是一个全面的服务框架,支持多种Web服务规范,如SOAP、RESTful API、WS-*(如WS-Security、WS-...
Spring MVC、CXF和Web Service是企业级Java应用开发中的三个关键组件,它们分别在不同的层面上服务于构建高效、可扩展的Web应用程序。 Spring MVC,全称Spring Model View Controller,是Spring框架的一部分,专为...