1、CXF配置和开发流程CXF的配置和开发流程第一种方式配置信息如下1、 cxf用的jar包http:/cxf.apache.org/download.html可以下载到。2、 web.xml CXFServlet CXF Servlet org.apache.cxf.transport.servlet.CXFServlet 1 CXFServlet /services/* 3、配置文件applicationContext-service.xml(注意红色部分为配置文件新加配置) 配置文件applicationContext-client.xml 注意applicationContext-serv
2、ice.xml配置文件中使用的是标签4、服务类HelloService.javaWebServicepublic interface HelloService public TestResponse hello(WebParam(name=name)String name); 实现类HelloServiceImpl.javaTestResponse.java、TestList.java5、客户端类HelloClient.java6、拦截器MyInterceptor.java各部分代码如下Web.xml!DOCTYPE web-app PUBLIC -/Sun Microsystems, Inc
3、./DTD Web Application 2.3/EN contextConfigLocation WEB-INF/classes/applicationContext.xml org.springframework.web.context.ContextLoaderListener CXFServlet CXF Servlet org.apache.cxf.transport.servlet.CXFServlet 1 CXFServlet /services/* xsd text/xml applicationContext-service.xml applicationContext-c
4、lient.xml HelloService.javapackage com.fxb.webservice.service;import javax.jws.WebParam;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;import com.fxb.webservice.bean.TestResponse;WebServicepublic interface HelloService public TestResponse hello(
5、WebParam(name=name)String name);HelloServiceImpl.javapackage com.fxb.webservice.service.impl;import com.fxb.webservice.bean.TestList;import com.fxb.webservice.bean.TestResponse;import com.fxb.webservice.service.HelloService;public class HelloServiceImpl implements HelloService public TestResponse he
6、llo(String name) System.out.println(name); TestResponse testResponse = new TestResponse(); TestList testList = new TestList(); testList.setName(HelloServiceImpl); testResponse.setTestList(testList); return testResponse; TestResponse.java类package com.fxb.webservice.bean;import javax.xml.bind.annotati
7、on.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlRootElement;XmlRootElement(name = TestResponse)/设置一级子节点XmlAccessorType(XmlAccessType.FIELD)public class TestResponse protected TestList testList; public TestList getTestList() return testList; publi
8、c void setTestList(TestList testList) this.testList = testList; TestList.java类package com.fxb.webservice.bean;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlRootElement;XmlRootElement(name = TestList)/设置二级子节点XmlAcce
9、ssorType(XmlAccessType.FIELD)public class TestList /类的属性为三级子节点 protected String name; protected String age; public String getName() return name; public void setName(String name) this.name = name; public String getAge() return age; public void setAge(String age) this.age = age; MyInterceptor.javapack
10、age com.fxb.webservice.interceptor;import org.apache.cxf.interceptor.Fault;import org.apache.cxf.message.Message;import org.apache.cxf.phase.AbstractPhaseInterceptor;import org.apache.cxf.phase.Phase;import org.apache.log4j.Logger;public class MyInterceptor extends AbstractPhaseInterceptor private L
11、ogger log = Logger.getLogger(MyInterceptor.class); public MyInterceptor(String phase) super(phase); public MyInterceptor() super(Phase.RECEIVE); SuppressWarnings(static-access) public void handleMessage(Message msg) throws Fault String uri = String.valueOf(msg.get(msg.REQUEST_URI); StringBuffer bf =
12、 new StringBuffer(uri + .xml); msg.put(msg.REQUEST_URI, String.valueOf(bf); System.out.println(in my intercetptor11111111111111111111); System.out.println(msg.get(msg.REQUEST_URI); log.info(in my interceptor:); Override public void handleFault(Message message) super.handleFault(message); System.out.
13、println(handleFault2); HelloClient.javapackage com.fxb.webservice.client;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.fxb.webservice.bean.TestResponse;import com.fxb.webservice.service.HelloService;public class
14、 HelloClient public static void main(String args) ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String com/fxb/webservice/client/applicationContext-client.xml); HelloService client = (HelloService)context.getBean(client); TestResponse response = client.hello(Joe);Sy
15、stem.out.println(Response: + response.getTestList().getName(); System.exit(0); 通过客户端测试输出为Response: HelloServiceImpl通过http:/localhost:8080/CXFSpring/services/HelloWorldTwo/hello输出为第二种方式配置信息如下1、 cxf用的jar包http:/cxf.apache.org/download.html可以下载到。2、web.xml CXFServlet CXF Servlet org.apache.cxf.transport.servlet.CXFServlet 1 CXFServlet /service/* 3、配置文件applicationContext-service.xml(注意红色部分为配置文件新加配置)beans xmlns=http:/www.springframework.org/schema/beans xmlns:xsi=http:/www.w3.org/2001/XMLSchema-instance xmlns:jaxws=http:/cxf.apache.org
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2