cxf创建webservice(java)

利用cxf创建webservice。

1. 下载apache-cxf-2.5.3,解压缩。

2. 创建两个Web工程:CMService(可以不是Web工程),CM。

3. 依据情况添加cxf的包,以下是参考。如果提示缺少jar包,可以去cxf的lib目录寻找并放进去。

向CMService项目:

cxf-2.5.3.jar

wsdl4j-1.6.2.jar

xmlschema-core-2.0.2.jar

jetty-util-7.5.4.v20111024.jar

jetty-server-7.5.4.v20111024.jar

jetty-http-7.5.4.v20111024.jar

jetty-io-7.5.4.v20111024.jar

jetty-continuation-7.5.4.v20111024.jar

neethi-3.0.2.jar

客户端CM项目:

cxf-2.5.3.jar,

xmlschema-core-2.0.2.jar,

wsdl4j-1.6.2.jar,

neethi-3.0.2.jar

slf4j的jar包对版本有要求。如果项目中添加了hibernate等,其中包含的jar包与之不符出现异常,需搜索并更正。

我用的slf4j-jdk14-1.6.2.jar,slf4j-log4j12-1.6.2.jar,slf4j-api-1.6.2.jar

4. 编写service提供程序(示例):

请注意,永远不要取相同名字的方法(函数)!即便这两个方法不是同一个类中的!当然,这也包含重载。

(1)接口

package com.net.casco.cmail.wsv.serv.non;

import javax.jws.WebParam;
import javax.jws.WebService;

/******************
 * Webservice(Non-CMail): Invoker Log Authorization Service
 *
 * @author 60874
 *
 */
@WebService
public interface WNLogAuthServ {

	/***************
	 * whether the user have authority to handle(delete) logs.
	 *
	 * @param userid
	 * @return
	 */
	public Boolean handleAuth(@WebParam(name = "userid") String userid)
			throws Exception;
}

(2). 实现类

package com.net.casco.cmail.wsv.serv.non.impl;

import javax.jws.WebService;

import com.net.casco.cmail.wsv.serv.non.WNLogAuthServ;

@WebService(endpointInterface =
 "com.net.casco.cmail.wsv.serv.non.WNLogAuthServ", serviceName = "WNLogAuthServ")
public class WNLogAuthServImpl implements WNLogAuthServ {

	public Boolean handleAuth(String userid) throws Exception {
		// TODO Auto-generated method stub
		return true;
	}

}

(3)service发布。有许多种方式。一 main函数;二利用服务器容器(如tomcat)等。

先介绍第一种(这里发布了四个服务):

package com.net.casco.cmail.wsv.wspub;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.net.casco.cmail.wsv.serv.non.WNDepServ;
import com.net.casco.cmail.wsv.serv.non.WNLogAuthServ;
import com.net.casco.cmail.wsv.serv.non.WNUserDepServ;
import com.net.casco.cmail.wsv.serv.non.WNUserServ;
import com.net.casco.cmail.wsv.serv.non.impl.WNDepServImpl;
import com.net.casco.cmail.wsv.serv.non.impl.WNLogAuthServImpl;
import com.net.casco.cmail.wsv.serv.non.impl.WNUserDepServImpl;
import com.net.casco.cmail.wsv.serv.non.impl.WNUserServImpl;

/****************
 * Webservice Publish
 * @author 60874
 *
 */
public class WservPub {

	public static void main(String[] args) {
		try {
			WNDepServImpl wDepService = new WNDepServImpl();
			JaxWsServerFactoryBean jwsf = new JaxWsServerFactoryBean();
			jwsf.setServiceClass(WNDepServ.class);
			jwsf.setServiceBean(wDepService);
			jwsf.setAddress("http://localhost:9000/depthandle");
			jwsf.create();

			WNUserServImpl wUserService = new WNUserServImpl();
			JaxWsServerFactoryBean uBean = new JaxWsServerFactoryBean();
			uBean.setServiceClass(WNUserServ.class);
			uBean.setServiceBean(wUserService);
			uBean.setAddress("http://localhost:9000/userhandle");
			uBean.create();

			WNUserDepServImpl wudService = new WNUserDepServImpl();
			JaxWsServerFactoryBean udBean = new JaxWsServerFactoryBean();
			udBean.setServiceClass(WNUserDepServ.class);
			udBean.setServiceBean(wudService);
			udBean.setAddress("http://localhost:9000/userdepthandle");
			udBean.create();

			WNLogAuthServImpl wLogService = new WNLogAuthServImpl();
			JaxWsServerFactoryBean lBean = new JaxWsServerFactoryBean();
			lBean.setServiceClass(WNLogAuthServ.class);
			lBean.setServiceBean(wLogService);
			lBean.setAddress("http://localhost:9000/loghandle");
			lBean.create();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}

5. 运行main函数,在浏览器中输入发布的地址应该就可以访问了。

例如:

http://localhost:9000/loghandle?wsdl可以看到发布的wsdl文件

调用函数:

http://localhost:9000/loghandle/方法名/参数名/参数值
第二种方式以后整理填充这里。

6. 能够正常之后,将webservice生成供客户端使用的包

打开命令行,cd到cxf的bin目录,执行以下命令:

wsdl2java -d d:/webservice [-frontend jaxws21] http://localhost:9000/myservice?wsdl

-frontend jaxws21命令可以不加。但如果生成后放置到CM项目中有错误,则加上该命令重新生成。

7. 将生成的文件(位于d:/webservice目录)拷贝到客户端项目CM相应的包下面(与服务器端相同的目录)

在客户端编写代码以调用。这个有好几种方法,下面给出示例:

(1)

URL url = new URL("http://localhost:9000/depthandle?wsdl");
WNDepServService service = new WNDepServService(url);
WNDepServ wdepserv = service.getWNDepServPort();

在生成的文件里面就包含了WNDepServService,WNDepServ这样的类。即通过这样的方式调用了远程的服务。

打开WNDepServService可以看到这样的代码:

/**
 * This class was generated by Apache CXF 2.5.3
 * 2012-05-29T16:31:49.392+08:00
 * Generated source version: 2.5.3
 *
 */
@WebServiceClient(name = "WNDepServService",
                  wsdlLocation = "http://localhost:9000/depthandle?wsdl",
                  targetNamespace = "http://non.serv.wsv.cmail.casco.net.com/")
public class WNDepServService extends Service {

    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("http://non.serv.wsv.cmail.casco.net.com/", "WNDepServService");
    public final static QName WNDepServPort = new QName("http://non.serv.wsv.cmail.casco.net.com/", "WNDepServPort");
    static {
        URL url = null;
        try {
            url = new URL("http://localhost:9000/depthandle?wsdl");
        } catch (MalformedURLException e) {
            java.util.logging.Logger.getLogger(WNDepServService.class.getName())
                .log(java.util.logging.Level.INFO,
                     "Can not initialize the default wsdl from {0}", "http://localhost:9000/depthandle?wsdl");
        }
        WSDL_LOCATION = url;
    }

    public WNDepServService(URL wsdlLocation) {
        super(wsdlLocation, SERVICE);
    }

    public WNDepServService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

(2)

Service service = Service.create(Service_name);

Service_name为

new QName("http://non.serv.wsv.cmail.casco.net.com/", "WNDepServService")
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
WNDepServ wdserv = service.getPort(WNDepServ.class);
This entry was posted in Computer, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s