最近学习cxf,做个例子玩一下
服务端
一、首先创建一个动态Web Project
所有jar包如下图所示:
二、接口类
package org.cxf.spring.server;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface IHelloWorld {
@WebResult(name = "out")
public String execute(@WebParam(name = "name") String name);
public void m2(String parm);
}
三、实现类
package org.cxf.spring.server.impl;
import javax.jws.WebService;
import org.cxf.spring.server.IHelloWorld;
import org.springframework.stereotype.Service;
@Service("hw")
@WebService(endpointInterface = "org.cxf.spring.server.IHelloWorld", serviceName = "HelloWorld")
public class HelloWorld implements IHelloWorld {
/*
* (non-Javadoc)
*
* @see org.cxf.spring.server.IHelloWorld#execute(java.lang.String)
*/
@Override
public String execute(String name) {
// TODO Auto-generated method stub
System.out.println("您好! " + name);
return "您好!欢迎 【" + name + "】访问CXFSpring3.0WebService...";
}
/* (non-Javadoc)
* @see org.cxf.spring.server.IHelloWorld#m2(java.lang.String)
*/
@Override
public void m2(String parm) {
// TODO Auto-generated method stub
}
}
四、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>CXFSpring3.0WebService</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
五、application.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:xsd="http://www.w3.org/2001/XMLSchema" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <context:component-scan base-package="org.cxf.spring" /> <jaxws:endpoint id="webHelloWorld" implementor="#hw" address="/execute" publish="true"> </jaxws:endpoint> </beans>
六、服务端完成,最终项目结构图如下:
客户端
一、同样新建一个动态web project
jar包同服务端一样
二、创建服务类接口
package org.cxf.spring.server;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface IHelloWorld {
@WebResult(name = "out")
public String execute(@WebParam(name = "name") String name);
}
三、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>CXFSpring3.0WebServiceClient</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>webServiceClient</servlet-name> <servlet-class>org.cxf.spring.servlet.WebServiceClient</servlet-class> </servlet> <servlet-mapping> <servlet-name>webServiceClient</servlet-name> <url-pattern>/webServiceClient</url-pattern> </servlet-mapping> </web-app>
四、application.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:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="hw" class="org.cxf.spring.server.IHelloWorld"
factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="org.cxf.spring.server.IHelloWorld" />
<property name="address"
value="http://localhost:8080/CXFSpring3.0WebService/services/execute?wsdl" />
</bean>
</beans>
五、建一个servlet,以便测试
package org.cxf.spring.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.cxf.spring.server.IHelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Servlet implementation class WebServiceClient
*/
@WebServlet("/WebServiceClient")
public class WebServiceClient extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public WebServiceClient() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("this is CXFSpringServiceClient servlet...");
ApplicationContext ctx = new ClassPathXmlApplicationContext("config/application.xml");
IHelloWorld hw = (IHelloWorld) ctx.getBean("hw");
String result = hw.execute("Mr CXF");
System.out.println(result);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
七、客户端结构图
八、测试