WebService_Demo

简述

    使用IDEA开发webservice服务,从零开始一步一步指引你。


服务端开发

    首先创建一个webservice项目,如下图

    创建完项目后idea会帮我们创建一个类,helloword,我们把它删掉。

    接下来新建一个接口

 1 package com.webservice.demo;
 2 
 3 import javax.jws.WebService;
 4 
 5 /**
 6  * demo
 7  *
 8  * @author GaoFei
 9  * Create by 2018/1/31
10  */
11 @WebService
12 public interface DemoServer {
13     Double sum(Double a, Double b);
14 
15     Double minus(Double a, Double b);
16 
17     Double ride(Double a, Double b);
18 
19     Double divide(Double a, Double b);
20 }

    然后创建接口的实现类并实现加减乘除4个方法。

 1 package com.webservice.demo.impl;
 2 
 3 import com.webservice.demo.DemoServer;
 4 
 5 import javax.jws.WebService;
 6 import javax.xml.ws.Endpoint;
 7 
 8 /**
 9  * annotation
10  *
11  * @author GaoFei
12  * Create by 2018/1/31
13  */
14 @WebService(serviceName = "DemoServer", endpointInterface = "com.webservice.demo.DemoServer")
15 public class DemoServerImpl implements DemoServer {
16 
17     public static void main(String args[]) {
18         Endpoint.publish("http://localhost:9000/DemoServer", new DemoServerImpl());
19     }
20 
21     /**
22      * 加
23      *
24      * @author GaoFei
25      * Create by 2018-01-31
26      */
27     @Override
28     public Double sum(Double a, Double b) {
29         return a + b;
30     }
31 
32     /**
33      * 减
34      *
35      * @author GaoFei
36      * Create by 2018-01-31
37      */
38     @Override
39     public Double minus(Double a, Double b) {
40         return a - b;
41     }
42 
43     /**
44      * 乘
45      *
46      * @author GaoFei
47      * Create by 2018-01-31
48      */
49     @Override
50     public Double ride(Double a, Double b) {
51         return a * b;
52     }
53 
54     /**
55      * 除
56      *
57      * @author GaoFei
58      * Create by 2018-01-31
59      */
60     @Override
61     public Double divide(Double a, Double b) {
62         return a / b;
63     }
64 }

    然后修改/web/WEB-INF/sun-jaxws.xml。

1 <?xml version="1.0" encoding="UTF-8"?>
2 
3 <endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
4     <endpoint
5             name='DemoServer'
6             implementation='com.webservice.demo.impl.DemoServerImpl'
7             url-pattern='/services/DemoServer'/>
8 </endpoints>

    最后运行main方法。

    至此,服务端就开发完成了。

客户端开发

    首先新建一个webservice client项目。

    

    

    调用

    

     运行结果

    

   

原文地址:https://www.cnblogs.com/gaofei-1/p/8395089.html