webService (一)简单的服务端与客户端程序

package com.sg.service;

import javax.jws.WebService;

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

 

package com.sg.service;

import javax.jws.WebService;

 @WebService(endpointInterface="com.sg.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;
 }

}

 

package com.sg.service;

import javax.xml.ws.Endpoint;


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

 

客户端:

package com.sg.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 {
   //创建访问wsdl服务器地址的url
   URL url = new URL("http://localhost:8888/ns?wsdl");
   //通过Qname 指明服务的基本信息
   QName qName = new QName("http://service.sg.com/", "MyServiceImplService");
   //创建服务
   Service service = Service.create(url, qName);
   //实现接口
   IMyService ms = service.getPort(IMyService.class);
   System.out.println(ms.add(22, 33));
   
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
 }
 

 

 

<definitions targetNamespace="http://service.sg.com/" name="MyServiceImplService"><types><xsd:schema><xsd:import namespace="http://service.sg.com/" schemaLocation="http://localhost:8888/ns?xsd=1"/></xsd:schema></types><message name="minus"><part name="parameters" element="tns:minus"/></message><message name="minusResponse"><part name="parameters" element="tns:minusResponse"/></message><message name="add"><part name="parameters" element="tns:add"/></message><message name="addResponse"><part name="parameters" element="tns:addResponse"/></message><portType name="IMyService"><operation name="minus"><input message="tns:minus"/><output message="tns:minusResponse"/></operation><operation name="add"><input message="tns:add"/><output message="tns:addResponse"/></operation></portType><binding name="MyServiceImplPortBinding" type="tns:IMyService"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><operation name="minus"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation><operation name="add"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation></binding><service name="MyServiceImplService"><port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding"><soap:address location="http://localhost:8888/ns"/></port></service></definitions>

 

 

 

你可能感兴趣的:(webservice)