webservice helloworld案例

所需加入的jar包

cxf-2.6.1.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaxws_2.2_spec-1.1.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
jetty-continuation-7.5.4.v20111024.jar
jetty-http-7.5.4.v20111024.jar
jetty-io-7.5.4.v20111024.jar
jetty-server-7.5.4.v20111024.jar
jetty-util-7.5.4.v20111024.jar
neethi-3.0.2.jar
slf4j-api-1.6.2.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.2.jar

服务端的接口

1 @WebService
2 public interface HelloWorldService {
3 
4     public String sayhello(@WebParam(name = "name") String name);
5 
6 }

接口实现

 1 import javax.jws.WebService;
 2 
 3 @WebService
 4 public class HelloworldServiceImpl {
 5 
 6     public String sayhello(String name) {
 7         System.out.println("hello" + name);
 8         return "enen";
 9     }
10 
11 }

 

测试代码 1 import javax.xml.ws.Endpoint; 2

 3 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 4 import org.apache.cxf.phase.Phase;
 5 
 6 import com.testhelloworld.loggin.BaseLogging;
 7 import com.testhelloworld.service.HelloWorldService;
 8 import com.testhelloworld.service.HelloworldServiceImpl;
 9 
10 public class Helloworld {
11 
12     /**
13      * <b>function:</b>发布WebService
14      * 
15      * @author hoojo
16      */
     //这另外一种部署方式 17 public static void deployService() { 18 System.out.println("Server start ……"); 19 HelloworldServiceImpl service = new HelloworldServiceImpl(); 20 String address = "http://localhost:9000/WebServiceTest/helloworldserver"; 21 Endpoint.publish(address, service); 22 } 23 24 public static void main(String[] args) { 25 26 // deployService(); 27 28 JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); 29 factoryBean.setServiceClass(HelloWorldService.class); 30 factoryBean.setAddress("http://localhost:9000/WebServiceTest/helloworldserver"); 31 // factoryBean.getInInterceptors().add(new LoggingInInterceptor()); 32 // factoryBean.getInInterceptors().add(new LoggingOutInterceptor()); 33 // 添加请求和响应的拦截器,Phase.RECEIVE只对In有效,Phase.SEND只对Out有效 34 factoryBean.getInInterceptors().add(new BaseLogging(Phase.RECEIVE)); //这里我们使用的自己实现的logging实现当然也可以使用他们的 35 factoryBean.getOutInterceptors().add(new BaseLogging(Phase.SEND)); 36 HelloWorldService serviceImpl = (HelloWorldService) factoryBean.create(); 37 String name = serviceImpl.sayhello("666"); 38 System.out.println(name); 39 } 40 41 }

下面是BaseLogging的源码

 1 package com.testhelloworld.loggin;
 2 
 3 import org.apache.cxf.interceptor.Fault;
 4 import org.apache.cxf.message.Message;
 5 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 6 
 7 public class BaseLogging extends AbstractPhaseInterceptor<Message> {
 8 
 9     public BaseLogging(String phase) {
10         super(phase);
11     }
12 
13     @Override
14     public void handleMessage(Message arg0) throws Fault {
15         System.out.println("++++++++++++++++++");
16         System.out.println(arg0);
17         if (arg0.getDestination() != null) {
18             System.out.println(arg0.getId() + "#" + arg0.getDestination().getMessageObserver());
19         }
20         if (arg0.getExchange() != null) {
21             System.out.println(arg0.getExchange().getInMessage() + "#" + arg0.getExchange().getInFaultMessage());
22             System.out.println(arg0.getExchange().getOutMessage() + "#" + arg0.getExchange().getOutFaultMessage());
23         }
24 
25     }
26 
27 }

 

posted @ 2016-09-04 20:46 奋斗,坚持 阅读(...) 评论(...)  编辑 收藏