---
title: "cxf入门"
author: "stondeng"
date: "2012-01-19T12:29:35Z"
tags: ["Java", "WebService", "cxf"]
categories: ["编程语言"]
slug: "cxf"
draft: false
lastmod: "2012-01-19T12:29:35Z"
id: 1358315
url: "https://stonedeng.iteye.com/blog/1358315"
---

一、CXF官方网址：http://cxf.apache.org/，到这去下载最新的版本，解压
二、创建一个工程，从解压的lib文件夹下导入如下包：
    cxf-2.5.1.jar
    wsdl4j-1.6.2.jar
    xmlschema-core-2.0.1.jar
    jetty-server-7.5.3.v20111011.jar
    jetty-util-7.5.3.v20111011.jar
    jetty-continuation-7.5.3.v20111011.jar
    jetty-http-7.5.3.v20111011.jar
    jetty-io-7.5.3.v20111011.jar
    neethi-3.0.1.jar
三、创建服务器端代码
  1、接口Login.java
  
```java

package cxf.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface Login {
	public String login(@WebParam(name="name")String name,@WebParam(name="password")String password);
}

```

  2、实现类LoginImpl.java

```java

package cxf.service;

import javax.jws.WebService;


@WebService(endpointInterface="cxf.service.Login",serviceName="Login")
public class LoginImpl implements Login {

	public String login(String name, String password) {
		String result = "登录CXF 服务端成功!";
		if (!"cxf".equalsIgnoreCase(name) || !"cxf".equalsIgnoreCase(password)) {
			result = "用户名或密码不正确，请重新登录!";
		}
		return result;
	}

}

```

  3、发布

```java

package cxf.service;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class Pub {
	public static void main(String[] args) {
		LoginImpl loginImpl = new LoginImpl();
		JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
		bean.setServiceClass(Login.class);
		bean.setServiceBean(loginImpl);
		bean.setAddress("http://localhost:9000/helloWorld");
		bean.create();
		System.out.println("Finish....");
	}
}

```

将产生如下信息：

```java

2012-1-19 12:28:22 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf/}LoginService from class cxf.service.Login
2012-1-19 12:28:22 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:9000/helloWorld
2012-01-19 12:28:22.591:INFO:oejs.Server:jetty-7.5.3.v20111011
2012-01-19 12:28:22.669:INFO:oejs.AbstractConnector:Started SelectChannelConnector@localhost:9000 STARTING
2012-01-19 12:28:22.685:INFO:oejsh.ContextHandler:started o.e.j.s.h.ContextHandler{,null}
Finish....


```

四、创建客户端代码
  1、定制客户端调用WebService的接口，这个接口中的方法签名和参数信息可以从wsdl中的内容看到：Login.java

```java

package cxf.client;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace = "http://service.cxf/")
public interface Login {
	public String login(@WebParam(name="name")String name,@WebParam(name="password")String password);
}

```

  2、编写测试类：

```java

package cxf.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Client {
	public static void main(String[] args) {
		JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
		bean.setServiceClass(Login.class);
		bean.setAddress("http://localhost:9000/helloWorld");
		Login login = (Login) bean.create();
		System.out.println("result--->"+login.login("cxf", "cxf"));
	}
}

```

将产生如下信息：

```java

2012-1-19 12:28:55 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf/}LoginService from class cxf.client.Login
result--->登录CXF 服务端成功!

```