快速实现一个基于jws的webservice项目

本文详细介绍了如何使用Java搭建WebService服务器的过程,包括定义服务接口、创建服务实现类及发布服务等步骤,并展示了如何通过浏览器验证服务运行状态。此外,还提供了创建客户端调用WebService的方法。

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

一.服务器的建立
1.创建服务接口
我们常听说的 SEI(server endpoint intelface) 就是这的这个服务的接口

          package webservice.wb01;
import javax.jws.WebService;
@WebService
public interface IMyserver {
public int add(int a, int b);
public int minus(int a,int b);
}



2.创建服务的实现类 SIB(server implements bean)
         package webservice.wb01;
import javax.jws.WebService;
@WebService(endpointInterface="webservice.wb01.IMyserver" )
public class MyserverImpl implements IMyserver {


public int add(int a, int b) {
System. out.println("add" );
return a+b;
}

public int minus(int a, int b) {
System. out.println("minus" );
return a-b;
};
}


3.发布服务
          package webservice.wb01;
import javax.xml.ws.Endpoint;
public class Test {

public static void main(String[] args) {
String address= "http://localhost:9090/ns";
Endpoint. publish(address, new MyserverImpl());
}
}


在浏览器中访问http://localhost:9090/ns?wsdl 就可以看的服务运行的情况

<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions targetNamespace= "http://wb01.webservice/" name="MyserverImplService" >
<types>
<xsd:schema>
<xsd:import namespace= "http://wb01.webservice/" schemaLocation="http://localhost:9090/ns?xsd=1" />
</xsd:schema>
</types>
<message name= "minus">
<part name= "parameters" element= "tns:minus" />
</message>
<message name= "minusResponse">
<part name= "parameters" element= "tns:minusResponse" />
</message>
<message name= "add">
<part name= "parameters" element= "tns:add" />
</message>
<message name= "addResponse">
<part name= "parameters" element= "tns:addResponse" />
</message>
<portType name= "IMyserver">
<operation name= "minus">
<input message= "tns:minus"/>
<output message= "tns:minusResponse"/>
</operation>
<operation name= "add">
<input message= "tns:add"/>
<output message= "tns:addResponse"/>
</operation>
</portType>
<binding name= "MyserverImplPortBinding" type= "tns:IMyserver" >
<soap:binding transport= "http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name= "minus">
<soap:operation soapAction= ""/>
<input>
<soap:body use= "literal"/>
</input>
<output>
<soap:body use= "literal"/>
</output>
</operation>
<operation name= "add">
<soap:operation soapAction= ""/>
<input>
<soap:body use= "literal"/>
</input>
<output>
<soap:body use= "literal"/>
</output>
</operation>
</binding>
<service name= "MyserverImplService">
<port name= "MyserverImplPort" binding= "tns:MyserverImplPortBinding" >
<soap:address location= "http://localhost:9090/ns"/>
</port>
</service>
</definitions>




二。创建客户端
     package webservice.wb01;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class ClientTest {
public static void main(String[] args) {
try {
URL url= new URL("http://localhost:9090/ns" );
/***********************************************************
<definitions targetNamespace="http://wb01.webservice/" name="MyserverImplService">

QName中的第一个参数为wsdl中 targetNamespace的值,第二个参数为:name的值
***** ***********************************************/
QName qname= new QName("http://wb01.webservice/" , "MyserverImplService" );
Service service=Service. create(url, qname);
IMyserver iMyserver= service.getPort(IMyserver. class );

System. out .println("1+2= " + iMyserver.add(1, 2));
System. out .println("5-2= " + iMyserver.minus(5, 2));

} catch (Exception e) {

e.printStackTrace();
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值