webservice入门(一)

一:创建接口:
package com.gao.webservice;

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

@WebService 
public interface MyService {
	
	@WebResult(name="addResult")
	public int add(@WebParam(name="a")int a,@WebParam(name="b")int b);
	
	
	@WebResult(name="minusResult")
	public int minus(@WebParam(name="a")int a,@WebParam(name="b")int b);
}
二:创建实现类:
package com.gao.webservice;

import javax.jws.WebService;


@WebService(endpointInterface="com.gao.webservice.MyService")
public class MyServiceImpl implements MyService {

	@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 com.gao.webservice;

import javax.xml.ws.Endpoint;

public class service {
	public static void main(String[] args) {
		String address = "http://localhost:8877/ns";
		Endpoint.publish(address, new MyServiceImpl());
	}
}
四:测试类

package com.gao.webservice;

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:7777/ns?wsdl");
			QName qName = new QName("http://webservice.gao.com/","MyServiceImplService");
			Service service = Service.create(url, qName);
			MyService myService = service.getPort(MyService.class);
			System.out.println(myService.add(12,12));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}

 
 

你可能感兴趣的:(java,webservice)