您的位置:首页 > 其它

Head First WebService with CXF

2008-11-27 11:49 387 查看
1. Download apache cxf : http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip
Download Spring: http://www.springsource.org/download
2. Server side:
WEB-INF/web.xml -- web description
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring.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>/services/*</url-pattern>
</servlet-mapping>
</web-app>
WEB-INF/spring.xml
<?xml version="1.0"?>

<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" />

<bean id="sample" class="jax.ws.test.WebServiceSampleImpl">
</bean>

<jaxws:endpoint id="sampleEndPoint" implementor="#sample" address="/WebServiceSample" />

</beans>


web service interface WebServiceSample.java
package jax.ws.test;

import javax.jws.WebService;

/**
*
* @author kaven
*
*/
@WebService
public interface WebServiceSample {
String say(String hello);
}


web service impl WebServiceSampleImpl.java
package jax.ws.test;

import javax.jws.WebService;

@WebService(endpointInterface = "jax.ws.test.WebServiceSample")
public class WebServiceSampleImpl implements WebServiceSample {

public String say(String hello) {
System.out.println("Hello " + hello);
return "Hi buddy " + hello;
}

}

ant build file : build.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<project default="war" basedir=".">

<property name="dir.warroot" value="build/jaxWsTest.war"/>
<property name="dir.lib" value="WebContent/WEB-INF/lib" />
<!-- change this to your deploy directory -->

<property name="dir.jboss.delopy" value="/Users/kaven/Kaven/Resource/JBoss/jboss-4.2.3.GA/server/default/deploy"/>

<path id="build.classpath">
<fileset dir="${dir.lib}">
<include name="*.jar"/>
</fileset>
<pathelement location="dest"/>
</path>

<target name="clean">
<delete dir="build"/>
</target>

<target name="build" depends="clean">
<mkdir dir="build"/>
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes" classpathref="build.classpath">
</javac>
</target>

<target name="war" depends="build">
<mkdir dir="${dir.warroot}"/>
<mkdir dir="${dir.warroot}/WEB-INF"/>
<mkdir dir="${dir.warroot}/WEB-INF/classes"/>
<copy todir="${dir.warroot}/WEB-INF/classes">
<fileset dir="build/classes"/>
</copy>
<copy todir="${dir.warroot}/WEB-INF">
<fileset dir="WebContent/WEB-INF"/>
</copy>
</target>

<target name="deploy" depends="war">
<copy todir="${dir.jboss.delopy}/jaxWsTest.war">
<fileset dir="${dir.warroot}">
<include name="**/*"/>
</fileset>
</copy>
</target>
</project>
run ant deploy, the webservice will be deployed in your web container.

browser: http://127.0.0.1:8080/jaxWsTest/services/WebServiceSample?wsdl if you see the wsdl file means the webservice is deployed successfully

3. client side:
Thereare many ways to access webservice, for instance.
I. WSDL2Java generated Client
II. JAX-WS Proxy
III. JAX-WS Dispatch APIs
To make it simple, we use Proxy to invoke webservice
WebServiceClient.java
package jax.ws.test.client;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import jax.ws.test.WebServiceSample;

public class WebServiceClient {

public static void main(String[] args) throws MalformedURLException {
URL wsdlURL = new URL("http://127.0.0.1:8080/jaxWsTest/services/WebServiceSample?wsdl");
QName SERVICE_NAME = new QName("http://test.ws.jax/", "WebServiceSampleImplService");
Service service = Service.create(wsdlURL, SERVICE_NAME);
WebServiceSample client = service.getPort(WebServiceSample.class);
String result = client.say("kaven");
System.out.println("Response from server : "+result);
}
}

4. Advanced Tech
I. Configure the client invoking ConnectionTimeout and ReceiveTimeout
Client client = (Client) ClientProxy.getClient(port);
HTTPConduit http = (HTTPConduit) client.getConduit();

HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();

httpClientPolicy.setConnectionTimeout(300); // set ConnectionTimeout

httpClientPolicy.setAllowChunking(false);
httpClientPolicy.setReceiveTimeout(3200); // set ReceiveTimeout

http.setClient(httpClientPolicy);

more info refer to :http://cwiki.apache.org/CXF20DOC/client-http-transport-including-ssl-support.html

II. 那么多的jar包,是不是很晕呢, 刚才花了几个小时,终于找到了Java客户端调用CXF Web-Service最小Jar包子集。








图中所示的都是必要的,缺一不可,如果是standalone那么jboss下的两个必要的,如果在Jboss container中,可以不要
III. Asynchronous WebService Calling
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: