//localhost:
8080/CXFSpring/services/HelloWorldTwo"/>
--ENDSNIPPET:
beans-->
HelloService.java
packagecom.fxb.webservice.service;
importjavax.jws.WebParam;
importjavax.jws.WebService;
importjavax.jws.soap.SOAPBinding;
importjavax.jws.soap.SOAPBinding.Style;
importcom.fxb.webservice.bean.TestResponse;
@WebService
publicinterfaceHelloService{
publicTestResponsehello(@WebParam(name="name")Stringname);
}
HelloServiceImpl.java
packagecom.fxb.webservice.service.impl;
importcom.fxb.webservice.bean.TestList;
importcom.fxb.webservice.bean.TestResponse;
importcom.fxb.webservice.service.HelloService;
publicclassHelloServiceImplimplementsHelloService{
publicTestResponsehello(Stringname){
System.out.println(name);
TestResponsetestResponse=newTestResponse();
TestListtestList=newTestList();
testList.setName("HelloServiceImpl");
testResponse.setTestList(testList);
returntestResponse;
}
}
TestResponse.java类
packagecom.fxb.webservice.bean;
importjavax.xml.bind.annotation.XmlAccessType;
importjavax.xml.bind.annotation.XmlAccessorType;
importjavax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="TestResponse")//设置一级子节点
@XmlAccessorType(XmlAccessType.FIELD)
publicclassTestResponse{
protectedTestListtestList;
publicTestListgetTestList(){
returntestList;
}
publicvoidsetTestList(TestListtestList){
this.testList=testList;
}
}
TestList.java类
packagecom.fxb.webservice.bean;
importjavax.xml.bind.annotation.XmlAccessType;
importjavax.xml.bind.annotation.XmlAccessorType;
importjavax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="TestList")//设置二级子节点
@XmlAccessorType(XmlAccessType.FIELD)
publicclassTestList{
//类的属性为三级子节点
protectedStringname;
protectedStringage;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetAge(){
returnage;
}
publicvoidsetAge(Stringage){
this.age=age;
}
}
MyInterceptor.java
packagecom.fxb.webservice.interceptor;
importorg.apache.cxf.interceptor.Fault;
importorg.apache.cxf.message.Message;
importorg.apache.cxf.phase.AbstractPhaseInterceptor;
importorg.apache.cxf.phase.Phase;
importorg.apache.log4j.Logger;
publicclassMyInterceptorextendsAbstractPhaseInterceptor{
privateLoggerlog=Logger.getLogger(MyInterceptor.class);
publicMyInterceptor(Stringphase){
super(phase);
}
publicMyInterceptor(){
super(Phase.RECEIVE);
}
@SuppressWarnings("static-access")
publicvoidhandleMessage(Messagemsg)throwsFault{
Stringuri=String.valueOf(msg.get(msg.REQUEST_URI));
StringBufferbf=newStringBuffer(uri+".xml");
msg.put(msg.REQUEST_URI,String.valueOf(bf));
System.out.println("inmyintercetptor11111111111111111111");
System.out.println(msg.get(msg.REQUEST_URI));
log.info("inmyinterceptor:
");
}
@Override
publicvoidhandleFault(Messagemessage){
super.handleFault(message);
System.out.println("handleFault@@@@@@@@@@@@@@@@@@@@@2");
}
}
HelloClient.java
packagecom.fxb.webservice.client;
importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcom.fxb.webservice.bean.TestResponse;
importcom.fxb.webservice.service.HelloService;
publicclassHelloClient{
publicstaticvoidmain(String[]args){
ClassPathXmlApplicationContextcontext
=newClassPathXmlApplicationContext(newString[]{"com/fxb/webservice/client/applicationContext-client.xml"});
HelloServiceclient=(HelloService)context.getBean("client");
TestResponseresponse=client.hello("Joe");
System.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
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
—路径根据需要自己定义-->
/service/*
3、配置文件applicationContext-service.xml(注意红色部分为配置文件新加配置)
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
jaxws="http:
//cxf.apache.org