从零开始学习WebService--1

概念性东东

webservice目的:异构平台之间的交互

JAX-WS:Java Api XML Web Service(基于JAVAAPI XML实现的WebService)

SEI:Service Endpoint Interface(实例中的IMyService)

SIB:Service Implements Bean(实例中的MyServiceImpl)


一个简单的WebService实例

1 服务器的建立

1.1 创建接口

package com.lul.service;

import javax.jws.WebService;

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


1.2 创建实现类

package com.lul.service;

import javax.jws.WebService;

@WebService(endpointInterface="com.lul.service.IMyService")
public class MyServiceImpl 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);
	}
}


1.3 开启服务

package com.lul.service;

import javax.xml.ws.Endpoint;

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

启动成功后通过

从零开始学习WebService--1_第1张图片

从零开始学习WebService--1_第2张图片

2 客户端的建立

package com.lul.service;


import java.net.MalformedURLException;
import java.net.URL;


import javax.xml.namespace.QName;
import javax.xml.ws.Service;


public class TestClient {


	public static void main(String[] args) {
		try {
			URL url=new URL("http://localhost:8888/ns?wsdl");
			QName sname=new QName("http://service.lul.com/","MyServiceImplService");
			Service service=Service.create(url, sname);
			
			IMyService ms=service.getPort(IMyService.class);
			System.out.println(ms.add(10, 9));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}
运行结果:

从零开始学习WebService--1_第3张图片

你可能感兴趣的:(webservice,jax-ws)