Web Service示例Demo

服务端(Java): 

package com.ningshe.module.user.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

import com.jfinal.plugin.activerecord.Db;

@WebService
public class WebServiceDemo {

    @WebMethod(operationName = "getMsg")
    @WebResult(name = "ret")
    public String getMsg() {
        return "获取消息";
    }

    @WebMethod(operationName = "getUserCount")
    @WebResult(name = "result")
    public Long getUserCount() {
        return Db.queryLong("SELECT COUNT(*) FROM role_user");
    }

    @WebMethod(operationName = "updateOrder")
    @WebResult(name = "result")
    public String updateOrder(@WebParam(name = "reqParams") String reqParams) {
        System.out.println(reqParams);
        return reqParams;
    }

    @WebMethod(operationName = "createOrder")
    @WebResult(name = "result")
    public String createOrder(@WebParam(name = "reqParams") String reqParams) {
        System.out.println(reqParams);

        return reqParams;
    }

    public static void main(String[] args) {
        // 发布WebService服务
        Endpoint.publish("http://192.168.2.22:2019/ws_server/mallsrv", new WebServiceDemo());
        System.out.println("发布WebService服务成功...");

        // 浏览器访问 http://localhost:2019/ws_server/mallsrv?wsdl
    }

}

客户端(Java):

先用JDK工具wsimport,拉取远程代码,如:

wsimport -keep http://192.168.2.22:2019/ws_server/mallsrv?wsdl

测试代码: 

package test;

import com.ningshe.module.user.service.WebServiceDemo;
import com.ningshe.module.user.service.WebServiceDemoService;

public class TestWs {
    public static void main(String[] args) {
        WebServiceDemoService factory = new WebServiceDemoService();
        WebServiceDemo servicePort = factory.getWebServiceDemoPort();
        System.out.println(servicePort.getMsg());
        System.out.println(servicePort.getUserCount());
        
        System.out.println(servicePort.updateOrder("<xml><o>D8329048293084032<o/><xml/>"));
        
        
        // JDK 命令   
        //  wsimport  -keep  [url]
    }
}

客户端(Node.js)

先安装依赖:npm i soap

var soap = require('soap');
var url = 'http://192.168.2.22:2019/ws_server/mallsrv?wsdl';

var args = { reqParams: '<xml><onumber>D487347293749823</onumber></xml>' };

var soaptest = function() {
    soap.createClient(url, function(err, client) {
        client.getMsg(function(err, result) {
            console.log(result);
        });
        client.getUserCount(function(err, result) {
            console.log(result);
        });
        client.createOrder(args, function(err, result) {
            console.log(result);
        });
    });
};
soaptest();

 特别注意:在服务器环境,启动Web Service服务时,可能报IP绑定异常,可以尝试绑定内网的IPv4地址,然后用公网IP测试访问!我这里貌似就是阿里云的内网IP地址。

发布了79 篇原创文章 · 获赞 40 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/u013727805/article/details/103610189