首页 > Web开发 > 详细

创建Web Service项目

时间:2020-11-10 19:48:42      阅读:38      评论:0      收藏:0      [点我收藏+]
使用AXIS框架
 
idea方式:
技术分享图片
技术分享图片
创建后 加入axis依赖包到输出目录,idea也会提示你进行这步操作
技术分享图片
技术分享图片
即可看到对外暴露的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 删掉这部分
技术分享图片
 技术分享图片
再次尝试,即可成功获取 服务
 

创建Web Service项目

原文:https://www.cnblogs.com/hlf522/p/13955341.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!