IDEA WebService 服务端 客户端 Demo

  • 时间:
  • 浏览:
  • 来源:互联网

1. idea 生成 WebService项目

2. 在src目录下增加自己的服务代码。

https://gitee.com/jin_0611/web-service-server

package com.duzq.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebService()
public class WebServiceDemo {

    /**
     * Demo 1,简单的 WebService 接口
     * @param name
     * @return
     */
    @WebMethod()
    public String demo1(String name) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String df = sdf.format(new Date());

        String result = df + " Hello, world, from " + name;
        System.out.println(result);
        return result;
    }

    /**
     * Demo 2,项目中实际会发生的调用
     * @param request 入参请求
     * @return res
     */
    @WebMethod(action = "getOrderInfo")
    public WebServiceResponse getOrderInfo(WebServiceRequest request) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String df = sdf.format(new Date());

        System.out.println(df + "getOrderInfo 被调用");
        // 默认返回结果失败
        WebServiceResponse response = new WebServiceResponse();
        response.setCode(500);

        // 参数校验
        if (request == null) {
            response.setMsg("请求信息不能为空");
            return response;
        }
        if (request.getOrderInfo() == null) {
            response.setMsg("查询条件不能为空");
            return response;
        }
        if (request.getOrderInfo().getOrderId() == null) {
            response.setMsg("订单ID不能为空");
            return response;
        }
        // 成功处理场景
        response.setCode(200);
        // 模拟实际业务处理...
        OrderInfo orderInfo = getOrderInfo(request.getOrderInfo().getOrderId());
        response.setOrderInfo(orderInfo);
        return response;
    }

    /**
     * 模拟DB查询
     * @param orderId
     * @return
     */
    private OrderInfo getOrderInfo(Integer orderId) {
        if (orderId != null && !Integer.valueOf(1).equals(orderId)) {
            return null;
        }
        OrderInfo res = new OrderInfo();
        res.setOrderId(1);
        res.setOrderCode("D1001");
        res.setBuyerName("张三");
        res.setProductName("冰可乐");
        res.setSellerName("李四小卖部");
        res.setTotalWeight(new BigDecimal(1));
        res.setTotalMoney(new BigDecimal(6.5));
        res.setCreateDate(new Date());
        return res;
    }

    public static void main(String[] argv) {
        // 发布服务
        Object implementor = new WebServiceDemo();
        String address = "http://localhost:9000/WebServiceDemo";
        Endpoint.publish(address, implementor);
        System.out.println("服务开启...");
    }
}

发布后的预览效果

wsdl地址(后续生成代码使用):http://localhost:9000/WebServiceDemo?wsdl

3. WebService客户端代码,创建一个空的项目,然后转换成Maven项目

4. pom 文件引入以下 jar 包,创建相应jar包目录

    <dependencies>
        <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/axis/axis-jaxrpc -->
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
    </dependencies>

5. 选中目录生成 java bean文件

6. 书写测试方法

import com.alibaba.fastjson.JSONObject;
import com.duzq.ws.*;

public class MainTest {
    public static void main(String[] args) throws Exception {
        // 1. 相当于初始化对象
        WebServiceDemoService wsd = new WebServiceDemoServiceLocator();

        // 2. 获取可以用于请求 WebService 的对象
        WebServiceDemo wsdPort = wsd.getWebServiceDemoPort();

        // 3. 简单接口调用示例
        String demo1Res = wsdPort.demo1("简单调用测试");
        System.out.println("demo1 请求结果:" + demo1Res);

        // 4. 正常接口调用示例
        OrderInfo reqOrderInfo = new OrderInfo();
        reqOrderInfo.setOrderId(1);

        WebServiceRequest request = new WebServiceRequest();
        request.setOrderInfo(reqOrderInfo);

        System.out.println("getOrderInfo##req##" + JSONObject.toJSONString(request));
        WebServiceResponse response = wsdPort.getOrderInfo(request);
        System.out.println("getOrderInfo##res##" + JSONObject.toJSONString(response));
    }
}

客户端代码:https://gitee.com/jin_0611/web-service-client

 


服务端代码:https://gitee.com/jin_0611/web-service-client

客户端代码:https://gitee.com/jin_0611/web-service-client

本文链接http://www.dzjqx.cn/news/show-617183.html