创建Web Service项目

使用AXIS框架
 
idea方式:
创建后 加入axis依赖包到输出目录,idea也会提示你进行这步操作
项目启动后访问 http://localhost:8080/AxisWebService/services
即可看到对外暴露的web services服务
 
 
编写客户端调用Web Service HelloWorld
需要配置log4j.properties
log4j.rootLogger = debug,stdout,D,E

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

Client
package example;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

import javax.xml.namespace.QName;
public class Client {
    public static void main(String[] args) {
        String url = "http://localhost:8080/AxisWebService/services/HelloWorld?wsdl";
        String method = "sayHelloWorldFrom";

        Service service = new Service();
        try {
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);
            //call.setOperationName(new QName("http://localhost:8080/AxisWebService/services/HelloWorld", method));
            call.setOperationName(method);
            call.addParameter("param1", org.apache.axis.Constants.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            call.setReturnType(org.apache.axis.Constants.XSD_STRING);

            String param1 = "JJ";
            // 调用获取返回值
            String result = (String) call.invoke(new Object[]{param1});
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

 

Client.java
package example;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

import javax.xml.namespace.QName;
public class Client {
    public static void main(String[] args) {
        String url = "http://localhost:8080/AxisWebService/services/HelloWorld?wsdl";
        String method = "sayHelloWorldFrom";

        Service service = new Service();
        try {
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);
            //call.setOperationName(new QName("http://localhost:8080/AxisWebService/services/HelloWorld", method));
            call.setOperationName(method);
            call.addParameter("param1", org.apache.axis.Constants.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            call.setReturnType(org.apache.axis.Constants.XSD_STRING);

            String param1 = "JJ";
            // 调用获取返回值
            String result = (String) call.invoke(new Object[]{param1});
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
 
运行客户端遇到错误: SAXException
找到server-config.wsdd 删掉这部分
 
再次尝试,即可成功获取 服务
 

posted on 2020-11-10 18:42  蒟蒻鸡  阅读(305)  评论(0编辑  收藏  举报