`
caixu52110
  • 浏览: 11506 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Maven+SrpingMVC+WebService

阅读更多

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

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

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

<!-- cxf配置 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-databinding-aegis</artifactId>
			<version>3.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
		<version>3.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.1.1</version>
		</dependency>

 

 新建一个接口类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配置文件  将接口暴露

<?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-3.1.xsd  
                                http://cxf.apache.org/jaxws   
                                http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />

	<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
	<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />

	<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
	<jaxws:server
		serviceClass="cn.wonhigh.retail.emall.api.HelloWorld"
		address="/helloWorld">
		<jaxws:serviceBean>
			<!-- 要暴露的 bean 的引用 -->
			<ref bean="helloWorld" />
		</jaxws:serviceBean>
		<jaxws:inInterceptors>
			<ref bean="inLoggingInterceptor" />
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
	</jaxws:server>

</beans>  

 通过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<br/>
	 * 详细请查询 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();
		}
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics