java 简单的创建webservice接口 和远程访问

本文介绍如何使用Java发布WebService及其实现远程访问的方法。通过示例代码展示了如何创建WebService服务并将其发布到指定地址,同时提供了客户端调用服务的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用jdk发布webservice 

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

/**
 *
 * @author lili
 */
@WebService
public class TestWebService {
    /** 供客户端调用方法  该方法是非静态的,会被发布
     * @param name  传入参数
     * @return  返回结果
     * */
    public String getValue(String name){
        System.out.println("okokokoko: "+name);
        return "hello "+name;
    };
    /**
     * 方法上加@WebMentod(exclude=true)后,此方法不被发布;
     * @param name
     * @return
     */
    @WebMethod(exclude=true)  
    public String getHello(String name){
        return "你好! "+name;
    }
    /** 静态方法不会被发布
     * @param name
     * @return
     */
    public static String getString(String name){
        return "再见!"+name;
    }
    //通过EndPoint(端点服务)发布一个WebService
    public static void main(String[] args) {
     /*参数:1,本地的服务地址;
           2,提供服务的类;
      */
     Endpoint.publish("http://192.168.1.4:8081/Service/ServiceHello", new TestWebService());
     System.out.println("发布成功!");
     //发布成功后 在浏览器输入 http://192.168.1.4:8081/Service/ServiceHello?wsdl
    }
}

 

 

2. java远程访问 webservice

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType; 

public static void main(String[] args) {
        try {
            String endpoint = URL;
            // 直接引用远程的wsdl文件  
            // 以下都是套路  
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(endpoint);
            call.setOperationName(new QName("http://testwebservice.mycompany.com/","Hello"));// WSDL里面描述的接口名称  
            call.addParameter("arg0", XMLType.XSD_DATE,ParameterMode.IN);// 接口的参数  “使用arg0” 服务端才能接收到数据
            call.setReturnType(XMLType.XSD_STRING);// 设置返回类型  
            String temp = "ddd"; //访问数据
            String result = (String) call.invoke(new Object[]{temp});
            // 给方法传递参数,并且调用方法  
            System.out.println("result is " + result);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
        return "";
    }

 

转载于:https://my.oschina.net/u/2428630/blog/1921584

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值