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>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值