Maven+SrpingMVC+WebService

阅读更多

怎么maven工程这里就不多说了。。  不会的童鞋百度吧。

这里说的webservice 采用CXF方式发布

首先在maven工程pom文件引用CXF依赖。


		
			org.apache.cxf
			cxf-rt-frontend-jaxws
			3.1.1
		
		
			org.apache.cxf
			cxf-rt-databinding-aegis
			3.1.1
		
		
			org.apache.cxf
			cxf-rt-transports-http
		3.1.1
		
		
			org.apache.cxf
			cxf-rt-transports-http-jetty
			3.1.1
		

 

 新建一个接口类HelloWorld

package cn.wonhigh.retail.emall.api;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
	@WebMethod String sayHello();
}

 HelloWorld实现类

package cn.wonhigh.retail.gms.api.service;

import javax.jws.WebService;

import org.springframework.stereotype.Service;
import cn.wonhigh.retail.emall.api.HelloWorld;

@WebService
@Service("helloWorld")
public class HelloWorldImpl implements HelloWorld {

	@Override
	public String sayHello() {
		// TODO Auto-generated method stub
		System.out.println("------------");
		return "Hello world!"; 
	}

}

 cxf配置文件  将接口暴露




	

	
	

	
	
		
			
			
		
		
			
		
		
			
		
	

  

 通过main方法启动webservice

package cn.wonhigh.retail.gms.api;

import javax.xml.ws.Endpoint;

import cn.wonhigh.retail.gms.api.config.DiamondLoaderContainer;
import cn.wonhigh.retail.gms.api.service.HelloWorldImpl;

/**
 * 服务的入口类
 * 因dubbo自动扩展点无法扩展,故自行实现一个扩展类,预先启动diamond下载配置后启动Spring
 * @author wei.b
 * @date Jun 24, 2014 6:21:00 PM
 * @version 0.5.2 
 * @copyright yougou.com 
 */
public class Main {
	
	/**
	 * @param args dubbo扩展点名称 如:spring、jetty、log4j、logback
* 详细请查询 dubbo包中META-INF\dubbo\com.alibaba.dubbo.container.Container * @see com.alibaba.dubbo.container.Container */ protected Main() throws Exception { // START SNIPPET: publish System.out.println("Starting Server"); HelloWorldImpl implementor = new HelloWorldImpl(); String address = "http://localhost:8111/helloWorld"; Endpoint.publish(address, implementor); // END SNIPPET: publish } public static void main(String[] args) throws Exception { new Main(); //new DiamondLoaderContainer().contextInitialized(); //com.alibaba.dubbo.container.Main.main(args); } }

 好了  到这一步启动main方法,在浏览器上输入http://localhost:8111/helloWorld?wsdl应该就能看到对应的信息了。

再说下怎么调用吧

package cn.wonhigh.retail.eamll.api.test;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.ClientImpl;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.service.model.BindingInfo;
import org.apache.cxf.service.model.ServiceInfo;

import javax.xml.namespace.QName;
import java.net.URL;
import javax.xml.ws.Service;

/**
 * 测试webService
 * 
 * @author cai.x
 * 
 */
public class WebserviceClient {
	private static final QName SERVICE_NAME = new QName("http://api.emall.retail.wonhigh.cn", "HelloWorldImplService"); 
	public static void main(String[] args) throws Exception {
		try {
			JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); 
		    Client client = dcf.createClient("http://localhost:8111/helloWorld?wsdl"); 
		            //sayHello 为接口中定义的方法名称   张三为传递的参数   返回一个Object数组 
		    QName opName = new QName("http://api.emall.retail.wonhigh.cn/", "sayHello");  // 指定到接口类所在包 
		    Object[] objects=client.invoke(opName);    
		    for (Object object : objects) {
		    	System.out.println(object);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(cxf,webservice,maven,spring)