webservice快速实例01

1、服务器的建立

  1.1、创建接口

  1.2、创建实现类

  1.3、开启服务

 2、webservice

  2.1、SEI(Service Endpoint Interface) : 服务提供的接口

      2.2、SIB(Service Implemention Bean) : 服务实现的Bean

3、程序简单实例

package org;

import javax.jws.WebService;

@WebService
public interface IMyService {
	public int add(int a,int b);
	public int minus(int a,int b);
}
package org;

import javax.jws.WebService;
import org.service.IMyService;

@WebService(endpointInterface="org.IMyService")
public class IMyServiceImpl implements IMyService {

	@Override
	public int add(int a, int b) {
		System.out.println(a+"+"+b+"="+(a+b));
		return a+b;
	}

	@Override
	public int minus(int a, int b) {
		System.out.println(a+"+"+b+"="+(a-b));
		return a-b;
	}
}
package org;

import javax.xml.ws.Endpoint;
import org.service.impl.IMyServiceImpl;

public class MyServer {
	public static void main(String[] args) {
		String address = "http://localhost:8888/ns";
		Endpoint.publish(address, new IMyServiceImpl());
	}
}

   3.1、启动MyServer

   3.2、访问http://localhost:8888/ns?wsdl

   3.3、main()模拟

  

package org;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import org.service.IMyService;

public class ClientTest {
	public static void main(String[] args) {
		try {
			//创建访问wsdl服务地址的url
			URL url = new URL("http://localhost:8888/ns?wsdl");
			//通过Qname指明服务的具体信息<definitions targetNamespace="http://org/" name="IMyServiceImplService">
			QName sname = new QName("http://org/", "IMyServiceImplService");
			//创建服务
			Service service = Service.create(url,sname);
			//实现接口
			IMyService ms = service.getPort(IMyService.class);
			System.out.println(ms.add(12,33));
			//以上服务有问题,依然依赖于IMyServie接口
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}

 这是本人学习webservice做的一些笔记,供大家参考,希望对你有用。

原文地址:https://www.cnblogs.com/yfding/p/4234493.html