cxf+web service(三)HelloWorld

(三)HelloWorld

 

准备工作

eclipse 3.6

tomcat 6

jdk 1.7

cxf 相关架包,spring相关架包,jdbc相关架包

 

创建一个工程,把架包添加到lib中

 
cxf+web service(三)HelloWorld
 

 

写一个接口

 

package com.cxf.test;

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

@WebService
public interface HelloWorld {
	String zhuce(String username, String password);
}

 写一个实现

 

 

package com.cxf.test;

import javax.jws.WebService;
/**
* @ WebService:申明为webservice的注解 
*   endpointInterface:要暴露的接口类
*   serviceName :    服务名
*/
@WebService(endpointInterface ="com.cxf.test.HelloWorld",
			serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
	
	public String zhuce(String username, String password) {
		String result = "";
		if(username.equals("admin")){
			result = "用户名已经存在";
		}
		else if (password.length() != 6) {
			result = "请输入6位数密码";
		}
		return result;
	}
}

 发布web service

 

 

package com.cxf.test;

import javax.xml.ws.Endpoint;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class service {

	public static void startService() throws Exception{
			//通过CXF内置的Jetty应用服务器发布
			//方法1:用CXF的JaxWsServerFactoryBean类进行发布。(需要CXF相关包)
			HelloWorldImpl hw = new HelloWorldImpl();
			JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
			factoryBean.setAddress("http://localhost:8080/cxf/HelloWorld");
			factoryBean.setServiceClass(HelloWorld.class);
			factoryBean.setServiceBean(hw);
			factoryBean.create();
			System.out.println("方法1");
			
			//方法2:使用Sun JAX-WS 2中Endpoint.publish进行发布
			/*Endpoint endpoint = Endpoint.publish("http://localhost:10080/cxf/HelloWorld", new HelloWorldImpl());
			System.out.println("方法2");*/
			
	}
	
	public static void main(String[] args) throws Exception{
		startService();
		System.out.println("Web Service 发布成功");
	}
}

 在浏览器里面输入http://localhost:10080/cxf/HelloWorld?wsdl,就可以看见XML文件了,控制台打印 发布成功.

 

你可能感兴趣的:(Web,CXF,service)