新建一个接口类
</pre><pre name="code" class="java">package com.iflyee.cxf; import javax.jws.WebService; @WebService public interface Ivo { public boolean vo(String username,int count); public int getvousernameCount(); public int getvocount(); public String build01(); public String xml(); }
package com.iflyee.cxf; import java.util.ArrayList; import javax.jws.WebService; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; @WebService public class Vo implements Ivo { //添加属性 private static int pt; private static int ut; public int getvocount() { // TODO Auto-generated method stub return pt; } public int getvousernameCount() { // TODO Auto-generated method stub return ut; } public boolean vo(String username, int count) { ut++; pt+=count; return true; } public String build01(){ //DocumentHelper提供了创建Document对象的方法 Document document = DocumentHelper.createDocument(); try { //添加节点信息 Element rootElement = document.addElement("modules"); //这里可以继续添加子节点,也可以指定内容 rootElement.setText("这个是module标签的文本信息"); Element element = rootElement.addElement("module"); for (int i = 0; i < 5; i++) { Element nameElement = element.addElement("name"); Element valueElement = element.addElement("value"); Element descriptionElement = element.addElement("description"); nameElement.setText("名称"+i); nameElement.addAttribute("language", "java"+i);//为节点添加属性值 valueElement.setText("值"+i); valueElement.addAttribute("language", "c#"+i); descriptionElement.setText("描述"+i); descriptionElement.addAttribute("language", "sql server"+i); } System.out.println(document.asXML()); //将document文档对象直接转换成字符串输出 } catch (Exception e) { e.printStackTrace(); } return document.asXML(); } public String xml(){ StringBuffer str = new StringBuffer(); str.append("<body>\n"); APIUtils ap = new APIUtils(); User user = new User(); java.util.List<Object> list = new ArrayList<Object>(); list.add(1); list.add("张胜男"); list.add("xs111"); list.add(2); list.add("张胜"); list.add("xs222"); list.add(3); list.add("胜男"); list.add("xs333"); for (int i = 0; i < 3; i++) { str.append(ap.getXMLModel(user,list)); } System.out.println(str); str.append("</body>"); APIUtils.num=0; return str.toString(); } }
package com.iflyee.cxf; public class User { private int id; private String userName; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.lzw.springcxf.auth; import java.util.List; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage>{ public AuthInterceptor() { //拦截器在调用方法之前拦截SOAP消息 super(Phase.PRE_INVOKE); System.out.println("11111111111111111111111111111111111"); } /** * @Description: 拦截器操作 * @param msg 被拦截到的SOAP消息 * @throws Fault */ @Override public void handleMessage(SoapMessage msg) throws Fault { System.out.println("=============================="); System.out.println("=====自定义拦截器======="); //获取SOAP消息的Header List<Header> headers = msg.getHeaders(); //如果没有Header if(headers == null || headers.size() < 1) { throw new Fault(new IllegalArgumentException("没有Header,拦截器实施拦截")); } //获取Header携带是用户和密码信息 Header firstHeader = headers.get(0); Element ele = (Element) firstHeader.getObject(); NodeList userIdEle = ele.getElementsByTagName("userId"); NodeList userPassEle = ele.getElementsByTagName("userPass"); if (userIdEle.getLength() != 1) { throw new Fault(new IllegalArgumentException("用户Id格式不对")); } if (userPassEle.getLength() != 1) { throw new Fault(new IllegalArgumentException("用户密码格式不对")); } //获取元素的文本内容 String userId = userIdEle.item(0).getTextContent(); String userPass = userPassEle.item(0).getTextContent(); if (!userId.equals("lyy") || !userPass.equals("123456")) { throw new Fault(new IllegalArgumentException("用户和密码不正确")); } } }
</pre><pre code_snippet_id="1652672" snippet_file_name="blog_20160419_9_4894787" name="code" class="java">package jp.co.service.impl; import jp.co.service.TestService; public class TestServiceImpl implements TestService { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public String SayHello() { <span style="white-space:pre"> </span>System.out.println("功能方法被调用!"); <span style="white-space:pre"> </span>return "Hello 这是一个简单的WebService实例"; <span style="white-space:pre"> </span>} }
调用方法
package com.testClient; import org.apache.cxf.frontend.ClientProxyFactoryBean; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import jp.co.service.TestService; public class TestClient { public static void main(String[] args) { //创建一个客户端的代理工厂 ClientProxyFactoryBean clientProxy = new ClientProxyFactoryBean() ; clientProxy.setServiceClass(TestService.class); clientProxy.setAddress("http://localhost:8080/WcxF/services/test"); TestService pic = (TestService)clientProxy.create(); System.out.println(pic.SayHello()); } }
bean.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"> <!-- 引入CXF的支持的文件,来源CXF的jar文件 --> <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="vo" implementor="com.iflyee.cxf.Vo" address="/vo"> <!-- 配置IN拦截器 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> <bean class="com.lzw.springcxf.auth.AuthInterceptor"></bean> </jaxws:inInterceptors> <!-- 配置OUT拦截器 --> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:outInterceptors> </jaxws:endpoint> </beans>
cxf-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:simple="http://cxf.apache.org/simple" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd"> <!--配置CXF的ws服务 --> <simple:server id="testservice" serviceClass="jp.co.service.TestService" address="/test"> <simple:serviceBean> <!-- 配置CXF的服务的实现 --> <bean class="jp.co.service.impl.TestServiceImpl"></bean> </simple:serviceBean> </simple:server> </beans>
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" 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"> <!--添加spring的支持--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/bean.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--添加CXF的框架--> <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> <display-name>WcxF</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> </web-app>
源码一:CXF+Spring+自定义拦截器 WebService实例源码下载:http://download.csdn.net/detail/qq_14996421/9495690
源码二:根据实体类装换xml源码下载 : http://download.csdn.net/detail/qq_14996421/9495688