Apache CXF: An Open Source Service Framework
|
snowind9
2007-09-12
package demo.hw.server;
import javax.xml.ws.Endpoint;
public class Server {
protected Server() throws Exception {
// START SNIPPET: publish
System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:9000/helloWorld";
Endpoint.publish(address, implementor);
// END SNIPPET: publish
}
public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
package demo.hw.client;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import demo.hw.server.HelloWorld;
public final class Client {
private static final QName SERVICE_NAME
= new QName("http://server.hw.demo/", "HelloWorld");
private static final QName PORT_NAME
= new QName("http://server.hw.demo/", "HelloWorldPort");
private Client() {
}
public static void main(String args[]) throws Exception {
Service service = Service.create(SERVICE_NAME);
// Endpoint Address
String endpointAddress = "http://localhost:9000/helloWorld";
// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
HelloWorld hw = service.getPort(HelloWorld.class);
System.out.println(hw.sayHi("World"));
}
}
package demo.hw.server;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
package demo.hw.server;
import javax.jws.WebService;
@WebService(endpointInterface = "demo.hw.server.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "Hello " + text;
}
}
|
|
|
snowind9
2007-09-12
我在弄web service会陆续的给大家提供一些相关代码。有兴趣的可以一起。这个叫cxf 是apache的框架 中文资料好像没有。
|
|
|
snowind9
2007-09-13
上边和下边的小例子在请求8080端口的时候会失败。不知道是bug还是什么。网上没有相关资料
|
|
|
snowind9
2007-09-13
Spring 中的简单hello XXX。
服务的类和上边的例子一样。
package demo.spring;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
package demo.spring;
import javax.jws.WebService;
@WebService(endpointInterface = "demo.spring.HelloWorld",serviceName="HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "Hello " + text;
}
}
客户端和client-bean.xml
package demo.spring.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import demo.spring.HelloWorld;
public final class Client {
private Client() {
}
public static void main(String args[]) throws Exception {
// START SNIPPET: client
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext(new String[] {"demo/spring/client/client-beans.xml"});
HelloWorld client = (HelloWorld)context.getBean("client");
String response = client.sayHi("Joe");
System.out.println("Response: " + response);
System.exit(0);
// END SNIPPET: client
}
}
<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="client" class="demo.spring.HelloWorld" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="demo.spring.HelloWorld"/> <property name="address" value="http://localhost:9090/helloWebService/helloWorld"/> </bean> </beans> 服务端,可以不用这个类,部署后直接启动服务器就可以了
package demo.spring.servlet;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
public class Server {
protected Server() throws Exception {
System.out.println("Starting Server");
org.mortbay.jetty.Server server = new org.mortbay.jetty.Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(9090);
server.setConnectors(new Connector[] {connector});
WebAppContext webappcontext = new WebAppContext();
webappcontext.setContextPath("/");
webappcontext.setWar("webapp");
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] {webappcontext, new DefaultHandler()});
server.setHandler(handlers);
server.start();
System.out.println("Server ready...");
server.join();
}
public static void main(String args[]) throws Exception {
new Server();
}
}
服务端的配置 <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> web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
|

