webservice接入调研

描述

Java开发WebService最重要的两个规范:

JSR-224 (JAX-WS:Java API for XML-Based Web Services ) ,主要使用soap协议,使用wsdl来描述;

JSR-311 (JAX-RS:The Java API for RESTful Web Services),使用wadl描述;

对于使用JAX-RS规范定义的webservice,直接走http协议接入数据就行了。返回的格式有两种,json和xml. 

对于使用JAX-WS规范定义的webservice,接入数据流程会比较复杂,本文主要描述此种方式接入过程

接入过程

生成class文件

根据服务端提供的wsdl地址,生成class文件,生成方式采用jdk自带的wsimport命令,如:

wsimport -keep -d D:\test\src3 -s D:\test\src4 -p com.map -verbose http://localhost:9999/userService?wsdl

参数详情可以参考:wsimport生成客户端+客户端调用+客户端修改adress location - 职场菜鸡 - 博客园

获取所有的port

private static List<String> getAllPorts(Set<Class<?>> classes) {

    List<String> ports = new ArrayList<>();

    for (Class<?> aClass : classes) {

        final WebServiceClient annotation = aClass.getAnnotation(WebServiceClient.class);

        if (null != annotation) {

            ports.add(aClass.getSimpleName());

        }

    }

    return ports;

}

根据port获取所有的method

private static List<String> getMethodsByPort(Set<Class<?>> classes, String port)

throws ClassNotFoundException {

    List<String> methodNames = new ArrayList<>();

    Class<?> portClass = null;

    for (Class<?> aClass : classes) {

        if (aClass.getSimpleName().equals(port)) {

            portClass = aClass;

            break;

        }

    }

    final Method[] methods = portClass.getMethods();

    for (Method method : methods) {

        final WebEndpoint annotation = method.getAnnotation(WebEndpoint.class);

        final int parameterCount = method.getParameterCount();

        if (null != annotation && parameterCount == 0) {

            final Class<?> returnType = method.getReturnType();

            final Method[] returnTypeMethods = returnType.getMethods();

            for (Method returnTypeMethod : returnTypeMethods) {

                methodNames.add(returnTypeMethod.getName());

            }

        }

    }

    return methodNames;

}

调用method返回结果

private static Object invokeMethod(

Set<Class<?>> classes, String port, String methodName, List<String> params) throws Exception {

    Class<?> portClass = null;

    for (Class<?> aClass : classes) {

        if (aClass.getSimpleName().equals(port)) {

            portClass = aClass;

            break;

        }

    }

    final Method[] methods = portClass.getMethods();

    final Object service = portClass.newInstance();

    final Object getUserService2ImplPort =

    MethodUtils.invokeMethod(service, "getUserService2ImplPort", null);

    for (Method method : methods) {

        final WebEndpoint annotation = method.getAnnotation(WebEndpoint.class);

        final int parameterCount = method.getParameterCount();

        if (null != annotation && parameterCount == 0) {

            final Class<?> returnType = method.getReturnType();

            final Method[] returnTypeMethods = returnType.getMethods();

            for (Method returnTypeMethod : returnTypeMethods) {

                if (returnTypeMethod.getName().equals(methodName)) {

                    final Class<?>[] parameterTypes = returnTypeMethod.getParameterTypes();

                    final Object[] objects = paramConvert(parameterTypes, params);

                    final Object o = MethodUtils.invokeMethod(

                        getUserService2ImplPort, returnTypeMethod.getName(), objects);

                    return o;

                }

            }

        }

    }

    return null;

}

private static Object[] paramConvert(Class<?>[] parameterTypes, List<String> params) {

    Object[] objects = new Object[parameterTypes.length];

    for (int i = 0; i < parameterTypes.length; i++) {

        final Object o = JSON.parseObject(params.get(i), parameterTypes[i]);

        objects[i] = o;

    }

    return objects;

}

调用

public static void main(String[] args) throws Exception {

//页面交互需要输入的信息:1.输入wsdl地址 2.选择某个port 3.选择某个method 4.输入请求参数(若参数是对象,则输入json格式,若参数是原始类型,输入单个值)

//后端:1.根据wsdl生成class文件(用wsimport)2.加载class,获取所有的port 3.根据port获取所有的method 4.根据用户选中的method和入参,invoke方法,拿到返回值

String rootClassPath = "D:\\test\\src3";

final Set<Class<?>> classes = loadClasses(rootClassPath);

final List<String> allPorts = getAllPorts(classes);

System.out.println("所有端口:" + allPorts);

final List<String> methodsByPort = getMethodsByPort(classes, allPorts.get(0));

System.out.println("第一个端口所有的接口:" + methodsByPort);

String userJson = "{\"id\":1,\"username\":\"jili\",\"city\":\"nanjing\"}";

String carJson = "{\"id\":1,\"name\":\"jili\",\"price\":32.32}";

final Object test2 =

invokeMethod(classes, allPorts.get(0), "test2", Lists.newArrayList(userJson, carJson));

System.out.println(test2);

}

pom.xml

<dependency>

<groupId>org.reflections</groupId>

<artifactId>reflections</artifactId>

<version>0.9.11</version>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-spring-boot-starter-jaxws</artifactId>

<version>3.2.6</version>

</dependency>

认证

采用用户名密码的方式认证

参考:[已解决]:java webservice 用户验证 (服务端 + 客户端)_q1054261752的博客-CSDN博客

前端交互

页面交互需要输入的信息:

  1. 输入wsdl地址
  2. 选择某个port

  3. 选择某个method

  4. 输入请求参数(若参数是对象,则输入json格式,若参数是原始类型,输入单个值),会根据第3步选择的method,返回给前端参数个数,每个参数对应一个输入框

  5. (可选)若有用户密码,还需提供用户名密码的输入框

后端逻辑

  1. 根据wsdl生成class文件(用wsimport)

  2. 加载class,获取所有的port

  3. 根据port获取所有的method

  4. 根据用户选中的method和入参,invoke方法,拿到返回值

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值