首页 > 代码库 > 01快速实现一个基于Jws的Webservice项目

01快速实现一个基于Jws的Webservice项目

webservice

  异构平台之间的交互如:.net、php、python、perl

  流行的框架:CXF、Axis、Metro

  JAVA提供的:JAX-WS

一、快速实现一个基于Jws的Webservice项目

  1、服务器的建立

    1.1创建接口

      

package com.bling.service;import javax.jws.WebService;@WebServicepublic interface IMyService {    public int sum(int a,int b);        public int minus(int a,int b);}

 

    1.2创建实现类

package com.bling.service;import javax.jws.WebService;@WebService(endpointInterface="com.bling.service.IMyService")public class MyServiceImpl implements IMyService {    @Override    public int sum(int a, int b) {        // TODO Auto-generated method stub        System.out.println(a+"+"+b+" = "+(a+b));        return a+b;    }    @Override    public int minus(int a, int b) {        // TODO Auto-generated method stub        System.out.println(a+"+"+b+" = "+(a-b));        return a-b;    }}

    1.3开启服务

package com.bling.service;import javax.xml.ws.Endpoint;public class MyService {    public static void main(String[] args) {        // TODO Auto-generated method stub        String address = "http://localhost:8888/ws";        Endpoint.publish(address, new MyServiceImpl());    }}

可以生成一个wsdl的文件在服务地址中

  2.客户端建立

    

package com.bling.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) throws MalformedURLException {        // TODO Auto-generated method stub        URL url = new URL("http://localhost:8888/ws?wsdl");        QName sname= new QName("http://service.bling.com/","MyServiceImplService");        Service service = Service.create(url,sname);        IMyService ms = service.getPort(IMyService.class);        System.out.println(ms.sum(10, 20));        System.out.println(ms.minus(10, 20));    }}

输出:

  30
  -10

GitBub源码地址:https://github.com/WebServcie/service_start