webService使用的小例子


啥都不说直接开始写代码,因为你看到这个文章就知道webservice是啥了,现在跟你说怎么用

服务器端


创建接口

@WebService
public interface HelloWord {
    String sayHi(String name);
}
实现这个接口
@WebService(endpointInterface="com.zmj.service.system.HelloWord")
public class HelloWordImpl implements HelloWord{

    @Override
    public String sayHi(String name) {
        return name+"您好";
    }
}
主函数
public class HelloWordMain {
    public static void main(String args[]){
        HelloWord hw = new HelloWordImpl();
        //调用Endpoint的publish方法发布Web Service
        Endpoint.publish("http://127.0.0.1:9091/serviceTest", hw);
        System.out.println("Web Service暴露成功!");
    }
}
服务器端的使用就分成两种了,一种是直接通过我们暴漏的url直接使用,另一种是把暴露的代码生成到本地,服务器端使用生成到本地的代码,下面分别作为介绍
直接main方法加上下面代码就行了
一.
     
        URL url = new URL("http://127.0.0.1:9091/serviceTest?wsdl");
        QName qname = new QName("http://impl.system.service.zmj.com/","HelloWordImplService");
        Service service = Service.create(url,qname);
        HelloWord helloWord = service.getPort(HelloWord.class);
        System.out.println(helloWord.sayHi("赵梦杰"));

二.第二种的使用首先你要把代码通过url生成到自己的编译器上去,由于我使用的是idea所以现在我们就之介绍idea的生成方式
Tools->webService->Generate Java Code From Wsdl
会有弹出框 记住Web Service PlatFrom 一定要选Glassfish /JAX_WS2.2 RI /Metro 1.X/JWSDP 2.2这一项因为这个是idea自带的选择其他的还需要下载等很麻烦

       HelloWordImplService factory = new HelloWordImplService();
       HelloWord helloWord = factory.getHelloWordImplPort();
       System.out.println(helloWord.sayHi("赵梦杰"));


亲测可用如果有问题可以一起交流分享。








WebService是一种用于实现分布式应用程序之间通信的技术,它基于标准的SOAP协议,采用HTTP协议作为传输协议,通过XML格式进行数据交换。在Java中,使用Java的WebService框架可以轻松地创建和调用Web服务。 下面是一个Java中使用WebService调用的例子: 1. 首先,在Java中创建一个基于WebService的项目。 2. 定义一个接口,用于描述Web服务的方法。 ```java package com.example.webservice; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHello(String name); } ``` 3. 实现接口,编写具体的方法实现。 ```java package com.example.webservice; import javax.jws.WebService; @WebService(endpointInterface = "com.example.webservice.HelloWorld") public class HelloWorldImpl implements HelloWorld { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 4. 配置Web服务,使用Java的WebService框架进行发布。 ```java package com.example.webservice; import javax.xml.ws.Endpoint; public class HelloWorldPublisher { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/hello", new HelloWorldImpl()); System.out.println("Web service is running..."); } } ``` 5. 编写客户端代码,调用Web服务。 ```java package com.example.webservice; import javax.xml.namespace.QName; import javax.xml.ws.Service; import java.net.URL; public class HelloWorldClient { public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:8080/hello?wsdl"); QName qname = new QName("http://webservice.example.com/", "HelloWorldImplService"); Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); System.out.println(hello.sayHello("John")); } } ``` 以上是一个简单的Java中使用WebService调用的例子。通过定义接口和实现类,然后发布Web服务,最后在客户端通过生成的代理类进行WebService调用。我们可以看到,Java中使用WebService调用非常简单,只需要使用相应的注解、配置和代理类即可实现远程调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值