MyEclipse创建JAX-WS SOAP Web Services

1、创建Web Service项目

wKiom1M9fGGyUPGhAAKM-BFQaeQ254.jpg


2、创建一个普通Java类Calculator

wKiom1M9fN_AAWg6AADy1A5Et5g171.jpg

wKiom1M9fXCDiEeAAAIioRkUfqE433.jpg

wKioL1M9fZnhByi9AAIp5VYUyhc991.jpg

代码如下:

package com.xju.ws;
public class Calculator {
    public int add(int a, int b) {
        return (a + b);
    }
    public int subtract(int a, int b) {
        return (a - b);
    }
    public int multiply(int a, int b) {
        return (a * b);
    }
    public int divide(int a, int b) {
        return (a / b);
    }
    public String sayHello(String username){
        return getVersion(username);
    }
                                                                                                                                                                          
    private String getVersion(String str)
    {
        return str+",你好!";
    }
}


3、自下向上策略创建Web Service服务端

wKiom1M9f4Li_ZGnAAGpacAHIAk258.jpg

wKioL1M9f7GxueriAAKSHCRGWHs181.jpg


4、导入相关的 jar 包

wKioL1MtbRCirhR5AAK2lxXk_nk422.jpg

wKiom1M9gLujTbUVAAR_jzwUIDY566.jpg


5、部署并运行JAX-WS Web Service

   右键点击项目WebServiceProject,选择Debug As (or Run As)>MyEclipse Server Application来运行该项目。

wKioL1M9gneCFRN7AAPLCKTTkOg201.jpg


6、测试JAX-WS Web Service

   选择项目WebServiceProject,在工具栏上选择如下内容:

wKiom1M9g7byuB-PAACC7TFOa8Q088.jpg

   打开SOAP Web Services Explorer:

wKioL1M9hHWTz2BHAAIL9IbrzZE344.jpg

wKiom1M9hcLQqMG8AALUPPBCdm0996.jpg






创建JAX-WS Web Services Client

1、新建Java项目WebServiceClientProject

wKiom1M9h1uS484zAAKpsz7_sAw620.jpg

   选择WebServiceClientProject项目,从工具栏菜单中选择New Web Service Client,如下:

wKioL1M9iBfi5m8tAABPKUyICGs283.jpg

wKiom1M9iGiDwByPAAE75uLyLWQ077.jpg

wKiom1M9iN-g5RYHAAKClwu05t8747.jpg

wKiom1M9iWTzrhSeAAF2EBhc_So761.jpg

2、使用Web Service

   创建Java类WebServiceClient:

wKiom1M9ikXgjpaXAAJKO73I2Zs409.jpg

代码如下:

package com.xju.ws.client;
public class WebServiceClient {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CalculatorService service = new CalculatorService();
        CalculatorDelegate delegate = service.getCalculatorPort();
        System.out.println("1. 3+7=" + delegate.add(3, 7));
        System.out.println("2. 12-2=" + delegate.subtract(12, 2));
        System.out.println("3. 9*9=" + delegate.multiply(9, 9));
        System.out.println("4. 40/2=" + delegate.divide(40, 2));
        System.out.println("5. xju=" + delegate.sayHello("xju"));
    }
}

运行结果如下:

1. 3+7=10
2. 12-2=10
3. 9*9=81
4. 40/2=20
5. xju=xju,你好!






本文出自 “IT技术学习与交流” 博客,谢绝转载!

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