[b]Build Web Service by CXF[/b]
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.
[b]Web Services Standards Support:[/b] CXF supports a variety of web service standards including SOAP, the Basic Profile, WSDL, WS-Addressing, WS-Policy, WS-ReliableMessaging, and WS-Security.
[b]Frontends: [/b]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.
[b]Ease of use:[/b] 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.
[b]Binary and Legacy Protocol Support:[/b] 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.
[b]Server Side[/b]
----------------------------------------
1) Dependencies
2)
3)
4) beans.xml
5) web.xml
6) log4j.xml
7) Deploy to Tomcat
[b]Client side[/b]
----------------------------------------
1) Dependencies
The same as the server
2) server.properties
3) client-beans.xml
4) Web Service interface
5) Invoke web service
Note:
1) 如果返回/传递的参数是对象,要实现get/set 方法
2) 如果有方法重载,需要用@WebMethod标注一下operationName赋值为不同值
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.
[b]Web Services Standards Support:[/b] CXF supports a variety of web service standards including SOAP, the Basic Profile, WSDL, WS-Addressing, WS-Policy, WS-ReliableMessaging, and WS-Security.
[b]Frontends: [/b]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.
[b]Ease of use:[/b] 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.
[b]Binary and Legacy Protocol Support:[/b] 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.
[b]Server Side[/b]
----------------------------------------
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
[b]Client side[/b]
----------------------------------------
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);
}